I’ve been trying to make friendly urls for my site and have the following rules:
#enable rewrites
RewriteEngine on
RewriteBase /
# Redirect 404s
ErrorDocument 404 /404_not_found.cfm
# Redirect non www to www
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
# Add trailing slashes to uri
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/(images|js|css)
RewriteRule ^([^/.]+)$ /$1/ [R=301,L,NC]
# Redirect any request with page var to /var/ format
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^page=(.+[^/])$ [NC]
RewriteRule ^index\.cfm$ http://%{HTTP_HOST}/%1/ [R=301,L,NC]
# If not an existing file or directory
# Rewrite any request var/ to index.cfm?page=var
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ /index.cfm?page=$1 [QSA,L]
# property services
# Add trailing slash if not a file or directory and 301 redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^property-services/([^/.]+)$ http://%{HTTP_HOST}/property-services/$1/ [R=301,L,NC]
# Rewrite request any request with page var to property-services/var/
RewriteRule ^property-services/([^/.]+)/$ property-services/index.cfm?page=$1 [L,NC]
Everything works accept the final rule:
# Rewrite request any request with page var to property-services/var/
RewriteRule ^property-services/([^/.]+)/$ property-services/index.cfm?page=$1 [L,NC]
The page isn’t being loaded when I try to request:
http://www.example.co.uk/property-services/example-service/
If I remove the second to last and rule then I can get the page to load by requesting:
http://www.example.co.uk/property-services/example-service
The point is I need to load the page with the slash on the end as this is the format I have chosen for the site. Could someone explain where I am wrong please?
This rule will match any URL which ends with a single slash, (but not a double slash), so it will prevent your last rule from being reached.
I suspect you were needing something along the lines of: