Htaccess Negative Lookahead Issue

橙三吉。 提交于 2020-01-17 06:59:29

问题


This is a very specific issue to my hobby website.

I have this code in my htaccess:

RewriteRule ^(?!assets|404|i|chats|books|forum|blog|help|terms|policy|jobs|settings|reset|signup|signin|signout)([^/]+)$ profile_home.php?userdomain=$1 [L]
RewriteRule ^(?!assets|404|i|chats|books|forum|blog|help|terms|policy|jobs|settings|reset|signup|signin|signout)([^/]+)/([^/]+)$ profile_home.php?userdomain=$1&selection=$2 [L]

Now this for the most works perfectly, I can visit all of those links (url/404, url/i, etc fine and also visit (url/user1, url/user2)...

The problem comes from specifically going to one user so far that seems to be throwing a random 404. That user does exist and removing the lookahead fixes the problem and the user can be viewed, but of course those other links break.

I'm gonna provide some link examples directly for this issue:

/blog

/forum

/testingaccount

The above all work fine, but this very specific user does not work. And I can't even comprehend how it does this. The user exists, and the files etc all exist, but this one user is causing an issue.

/itsame or /forumerguy


回答1:


In your case, no url that begins with your exclusions will work: /i... /blog...

You can do that:

RewriteRule ^(assets|404|i|chats|books|forum|blog|help|terms|policy|jobs|settings|reset|signup|signin|signout)(/|$) - [L]

RewriteRule ^([^/]+)$ profile_home.php?userdomain=$1 [L]
RewriteRule ^([^/]+)/([^/]+)$ profile_home.php?userdomain=$1&selection=$2 [L]

Or if you need others rules after (assets|404|i|cha...), you can do that:

RewriteCond %{REQUEST_URI}  !^/(assets|404|i|chats|books|forum|blog|help|terms|policy|jobs|settings|reset|signup|signin|signout)(/|$)
RewriteRule ^([^/]+)$ profile_home.php?userdomain=$1 [L]
RewriteCond %{REQUEST_URI}  !^/(assets|404|i|chats|books|forum|blog|help|terms|policy|jobs|settings|reset|signup|signin|signout)(/|$)
RewriteRule ^([^/]+)/([^/]+)$ profile_home.php?userdomain=$1&selection=$2 [L]


来源:https://stackoverflow.com/questions/44089043/htaccess-negative-lookahead-issue

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