Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mode Rewrite; with/without trailing slash on end of url?

I basically tried this mode_rewrite rule below. It works with a slash on the end but I want it to work whether it has a trailing slash on the end or not. Basically I want it like this as some people see it as normal with a slash on the end and others don't hence why I want it to work whether it's there or not.

RewriteRule ^signup/register(.[^/]*) /signup/register.php [NC]

Basically it will work like http://localhost/signup/register/ but if I remove the / from the end it gives 404 error.

like image 376
PHPLOVER Avatar asked Feb 22 '11 15:02

PHPLOVER


3 Answers

...    
RewriteRule ^(url-rewrite)/?$ page.php [NC]
...

The ? after / specifies that there can be none or one / after your url-rewrite, as such it would accept it with or without the trailing /

like image 72
Joshua Avatar answered Dec 10 '22 19:12

Joshua


The subpattern .[^/]* requires at least one arbitrary character. In your case it’s probably that trailing slash.

You should better stick to one writing (either with or without trailing slash) and redirect th wrong writing to the proper, like:

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

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]
like image 38
Gumbo Avatar answered Dec 10 '22 21:12

Gumbo


plain old regexes:

RewriteRule ^signup/register/?$ /signup/register.php [NC]
like image 27
godzillante Avatar answered Dec 10 '22 19:12

godzillante