Rule with and without trailing slash

Topics: Developer Forum
Sep 12, 2007 at 7:40 AM
Hi guys,

I'd like to setup a rewrite rule which will successfully handle the following:

http://www.mysite.com/report --> http://www.mysite.com/index.cfm?event=report
http://www.mysite.com/report/ --> http://www.mysite.com/index.cfm?event=report

Currently I'm using {RewriteRule ^/(?!index\.cfm)(^\/\?+)$ /index.cfm?event=$1 L} but it only works for the 1st example (no trailing slash).

Any ideas how I can get a rule working for both scenarios?

Thanks again
Sep 12, 2007 at 1:24 PM


sharmo wrote:
Hi guys,

I'd like to setup a rewrite rule which will successfully handle the following:

http://www.mysite.com/report --> http://www.mysite.com/index.cfm?event=report
http://www.mysite.com/report/ --> http://www.mysite.com/index.cfm?event=report

Currently I'm using {RewriteRule ^/(?!index\.cfm)(^\/\?+)$ /index.cfm?event=$1 L} but it only works for the 1st example (no trailing slash).

Any ideas how I can get a rule working for both scenarios?

Thanks again


I wondered the same thing for a while, but the best way that I've found is this:

#--- Catch-all; if no trailing forward slash, redirect with one ---
RewriteRule ^/([^.?]+[^.?/])$ /$1/ [I,R=301]

This was posted here before, but it's been a while and can't remember who to credit.

So if someone visited {http://www.mysite.com/report} it would check for a trailing forward slash and if not found, it would redirect with one {http://www.mysite.com/report/}. Then you only need one rule - the one WITH the trailing forward slash.

Put this in the beginning of your INI file.

Hope that helps!