Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
828 views
in Technique[技术] by (71.8m points)

.htaccess - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...