mod-rewrite for URLs with extra characters after second slash

主宰稳场 提交于 2019-12-24 08:48:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!