在第二个斜杠后对带有多余字符的URL进行mod-rewrite [英] mod-rewrite for URLs with extra characters after second slash
问题描述
如何通过htaccess在第二个/
之后为所有带有多余字符的URL返回404 error
.
How to return 404 error
for all URL with extra characters after second /
by htaccess.
e.g1:
http://www.example.com/blah-blah/
不重定向(第2个/
之后没有多余的字符)
http://www.example.com/blah-blah/
NOT Redirect (no extra characters after 2th /
)
e.g2:
http://www.example.com/blah-blah/blah-blah
重定向到404
e.g3:
http://www.example.com/blah-blah/blah-blah/
重定向到404
e.g4:
http://www.example.com/blah-blah/blah-blah/blah-blah
重定向到404
e.g5:
http://www.example.com/blah-blah/blah-blah/blah-blah/
重定向到404
我尝试了此操作,但不适用于e.g3& e.g5(以斜杠结尾的网址):
I tried this but not works with e.g3 & e.g5 (URLs ending with slash):
ErrorDocument 404 /404
RewriteBase /
RewriteRule ^[^/]+/.+ - [R=404,L]
推荐答案
仅当第二个斜杠后有一些字符时才重定向请求,您需要此重写规则
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:当您在regexp后使用破折号时,它表示将匹配项传递给文件系统.
PS : when you're using a dash after regexp, it says to pass matches as it to filesystem.
EDIT :此模式/.*/.+与下一个示例匹配,但是我忘记了RewriteBase指令,因此您需要删除模式中的第一个斜杠(我之前编辑过RewriteRule)
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/=>不匹配
- /blah-blah/blah-blah =>匹配
- /blah-blah/blah-blah/=>匹配
- /blah-blah/blah-blah/blah-blah/=>匹配
PS:您可以使用此工具来检查您的Rewrite是否有效 rewrite-rule-tester
PS : You can use this tools, to check if your Rewrite works rewrite-rule-tester
这篇关于在第二个斜杠后对带有多余字符的URL进行mod-rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!