.htaccess same url with or without /

I'm doing painful rewriting of many URLs on the website I'm currently working on, but I noticed a small problem:

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

      

This line with navigation if I go to:

http://my-site/domains/transfer

      

But it won't work with a trailing /:

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

      

Is there any way to change the RewriteCode to accept any argument and go to the same page. It seems wasteful to have the same line twice to include '/'

Any help would be appreciated.

Greetings

+2


source to share


2 answers


Change the line as follows:

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

      



The magic is here: /?

and this allows the previous character to be used, in which case the forward slash ( /

) is optional.

If you want something to appear after transfer

, then remove the dollar sign ( $

) that marks the end of a valid match.

+3


source


I would recommend that you only allow one form of url, one with or without a trailing slash, and redirect if wrong:



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

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]

      

0


source







All Articles