views:

73

answers:

2

Howdy Guys, I am doing a painful rewrite of many urls on a website I am currently working on, but I have noticed a small problem:

RewriteRule ^domains/transfer$ ./cart.php?gid=11 [L,NC]

This line with navigate if I go to:

http://my-site/domains/transfer

But it won't work with a trailing /:

http://my-site/domains/transfer/

Is there a way I can change the RewriteCode to take either argument and go to the same page. It seems wasteful to have the same line twice to include a '/'

Any help would be appreciated.

Cheers

+3  A: 

Change the line to this:

RewriteRule ^domains/transfer/?$ ./cart.php?gid=11 [L,NC]

The magic is here: /? and that allows for the preceding character, in this case the slash (/), to be optional.

If you want something to come after the transfer, then remove the dollar sign ($) which marks the end of the allowable matching.

random
Awesome. Works perfectly. Thanks very much.
frodosghost
A: 

I would recommend you to allow just one URL form, the one with or without the trailing slash, and redirect if malformed:

# remove trailing slash
RewriteRule (.*)/$ /$1 [L,R=301]

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]
Gumbo
Is there a reason for allowing one but not the other? Is it a program issue, or more of just a correctness one?
frodosghost
@frodosghost: It is a correctness issue. Every resource should have only one valid URL.
Gumbo
@Gumbo :: Thanks. Shall implement.
frodosghost