Forum Moderators: phranque

Message Too Old, No Replies

url rewrite not working

can anyone help with a simple rewrite rule please?

         

evenbells

1:35 am on Jul 18, 2009 (gmt 0)

10+ Year Member



I'm trying to do a fairly simple rewrite to a querystring but for some reason it keeps giving me 404s when I browse.
What am I doing wrong?

#add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^/(.*)$ /$1/

#if its not a folder
RewriteCond %{SCRIPT_FILENAME} !-f

#or a directory
RewriteCond %{SCRIPT_FILENAME} !-d

#put the psuedo directories into querystring
RewriteRule ^/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?page=$1&category=$2&item=$3 [L]

If anyone has the time to answer I'd be very grateful

evenbells

1:37 am on Jul 18, 2009 (gmt 0)

10+ Year Member



$2 and $3 may not be present, depends on the view.

jdMorgan

3:49 am on Jul 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If any "pseudo-directory" part of the URL is missing, your rule will fail. You'll need three rules. Also, whenever possible, avoid checking the disk for file/directory exists, as it is a very resource0intensive operation for the server. As shown here in the modified first rule, you can usually tell if you need to add a trailing slash by whether there is a period (indicating a 'filetype') in the final path-part of the URL: If there is no period and no trailing slash, then add one.

Also, if you do need to do a file/directory exists check, then only do it once per request - Note how the last three rules now 'share' this check.


# Add missing trailing slash if no period in final URL-path-part
RewriteRule ^/(([^/]+/)*[^./]+)$ http://www.example.com/$1/ [R=301,L]
#
# Skip next 3 rules if final URL-path-part contains a
# period or if URL resolves to existing folder or file
RewriteCond %{REQUEST_URI} ^/([^/]+/)*[^./]+\.[^/]+$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^/ - [S=3]
#
# Rewrite pseudo-directories to script path with querystring
RewriteRule ^/([^/]+)/$ /index.php?page=$1 [L]
RewriteRule ^/([^/]+)/([^/]+)/$ /index.php?page=$1&category=$2 [L]
RewriteRule ^/([^/]+)/([^/]+)/([^/]+)/$ /index.php?page=$1&category=$2&item=$3 [L]

Jim