问题
How to return 404 error
for all URL with extra characters after second /
by htaccess.
e.g1:
http://www.example.com/blah-blah/
NOT Redirect (no extra characters after 2th /
)
e.g2:
http://www.example.com/blah-blah/blah-blah
Redirect to 404
e.g3:
http://www.example.com/blah-blah/blah-blah/
Redirect to 404
e.g4:
http://www.example.com/blah-blah/blah-blah/blah-blah
Redirect to 404
e.g5:
http://www.example.com/blah-blah/blah-blah/blah-blah/
Redirect to 404
I tried this but not works with e.g3 & e.g5 (URLs ending with slash):
ErrorDocument 404 /404
RewriteBase /
RewriteRule ^[^/]+/.+ - [R=404,L]
回答1:
in order to redirect request only if there is some characters after second slash, you need to this rewrite rule
ErrorDocument 404 /404
RewriteBase /
RewriteRule .*/.+ - [R=404,L]
PS : when you're using a dash after regexp, it says to pass matches as it to filesystem.
EDIT : this pattern /.*/.+ matches with next example, but I forgot RewriteBase directive, so you need to remove first slash in pattern (I edit RewriteRule before)
- /blah-blah/ => Not match
- /blah-blah/blah-blah => Match
- /blah-blah/blah-blah/ => Match
- /blah-blah/blah-blah/blah-blah/ => Match
PS : You can use this tools, to check if your Rewrite works rewrite-rule-tester
来源:https://stackoverflow.com/questions/38510845/mod-rewrite-for-urls-with-extra-characters-after-second-slash