Forum Moderators: phranque

Message Too Old, No Replies

Dynamic URLs

needs to work with or without trailing slash?

         

amwd07

10:17 pm on May 3, 2008 (gmt 0)

10+ Year Member



Hello

Please can someone help with the following:
I have 2 seperate RewriteRules for the frist condition
the below works but seems to effect all the other rules

anyone any idea's, have I maybe ovver complecated these rules?

Options +FollowSymLinks
RewriteEngine on

#Removes trailing slash from URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ [%{HTTP_HOST}...] [R=301,L]

RewriteRule ^([^.]+)$ /dynamic.php?feature=$1 [L]

(this only works if I comment the line below idealy need 1 rule to allow either $1 or $1/$2)

#RewriteRule ^([^.]+)/([^.]+)$ /dynamic.php?feature=$1&section=$2 [L]

I have tried to get the rules below to work by focing the trailing slash but this will not work with the above

# Force trailing slash on directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ $1/ [R,L]

RewriteRule ^dining/([^.]+)-([^.]+)\.php$ dining.php?county=$1&town=$2 [R,L]
RewriteRule ^([^.]+)/dining dining.php?town=$1 [L]
RewriteRule ^([^.]+)/eatingout/([^.]+) eatingout.php?town=$1&accountname=$2 [L]

jdMorgan

2:45 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your patterns are incorrect -- Don't use "[^.]+" everywhere, unless that's what you mean -- "one or more characters not equal to a literal period." See the regular-expressions tutorial cited in our Forum Charter for more information on regular-expressions patterns.

Put your rules in order, external redirects first, from most-specific to least-specific, then internal rewrites, again from most-specific to least-specific.

By checking for !-f, your "force a slash" rule adds a slash to any file that does not exist. Instead, I think you want to add a slash to any directory that would exist if the URL had a slash on it.


# Redirect to remove trailing slash from URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
#
# Redirect to add trailing slash on directories
RewriteCond %{REQUEST_FILENAME}/ -d
RewriteRule ^(.+[^/])$ http://%{HTTP_HOST}/$1/ [R=301,L]
#
# More-specific rewrites first
RewriteRule ^dining/([^/\-]+)-([^/.]+)\.php$ /dining.php?county=$1&town=$2 [R,L]
RewriteRule ^([^/]+)/dining$ /dining\.php?town=$1 [L]
RewriteRule ^([^/]+)/eatingout/([^/.]+)$ /eatingout.php?town=$1&accountname=$2 [L]
#
# Then least-specific "catch-alls"
RewriteRule ^([^/]+)/([^/.]+)$ /dynamic.php?feature=$1&section=$2 [L]
RewriteRule ^([^/.]+)$ /dynamic.php?feature=$1 [L]

This may not be exactly what you want, because you did not give example URL-paths, but it should be closer to working than what you had.

Jim