开发者

.htaccess same url with or without /

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开发者_如何学Go$ ./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


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.


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜