Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite issue with existing directory

         

LD_Dan

7:45 pm on Dec 16, 2009 (gmt 0)

10+ Year Member



Hi there,

I've tried searching for an answer to this but I must be honest I couldn't think of a search term for the problem, so I'll try and explain it best I can.

I'm trying to setup a fairly simple .htaccess file that will handle my clean URLs. For example:

www.domain.com/contact I want it to internally re-direct (is that the right term? basically so it doesn't change in the browser window) to www.domain.com/index.php?p=contact

That bit I've managed to do. The problem arises when I want to add a second level to the URL structure. So again for example:

www.domain.com/portfolio/example to www.domain.com/index.php?p=portfolio&e=example

This seems to work OK with the rule I have, UNLESS I leave the forward slash off the end of the URL, in which case it gets turned in to this:

www.domain.com/portfolio/example/?p=portfolio&e=example

I know I could add a / to the end of all my URLs but if a user types it in without one that isn't ideal. I don't know whether it's just a particular issue with 'portfolio' as a I do have a directory named that in the root. If it's causing the problem I could rename it I guess.

This is what I have so far in terms of my .htaccess:

RewriteRule ^/?portfolio/([a-z0-9_-]+)/?$ index.php?q=portfolio&e=$1 [NC,L]
RewriteRule ^([a-z0-9]+)/?$ index.php?q=$1 [NC,L]

I know the first rule isn't great but it's what I've been attempting to get around the issue (unsuccessfully mind you).

Any help would be welcome, I just can't seem to get it right with mod_rewrite :(

g1smd

9:30 pm on Dec 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What you have is a "rewrite". It accepts an incoming URL request (that matches the pattern) and fetches content from some place inside the server.

It is bad form for two different URLs to display the same content (and a URL with and without a slash is two different URLs) so you should redirect "with slash" requests to "without slash", and then rewrite only "without slash" requests to actually fetch the content.

There are some other steps you need to take in order to stop the parameter-based URLs being indexed should they ever be discovered, but that is something to be fixed after your initial problems are sorted.

jdMorgan

10:26 pm on Dec 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest you get rid of trailing slashes on all URLs which do not refer to directories or to directory-indexes. A trailing slash has a specific 'meaning' in HTTP and isn't "just another character":

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
#
# Redirect to remove trailing slashes unless requested URL resolves to an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://www.example.com/$1 [R=301,L]
#
# ... Index page URL and domain canonicalization redirect rules (if any) go here ...
#
RewriteRule ^([a-z0-9]+)/([a-z0-9_\-]+)$ index.php?q=$1&e=$2 [NC,L]
RewriteRule ^([a-z0-9]+)$ index.php?q=$1 [NC,L]

Note the Options directive arguments -- they may actually be more relevant to your problem than the code...

Jim