Htaccess Negative Lookahead Issue

2019-08-16 18:30发布

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

标签: php .htaccess
1条回答
叛逆
2楼-- · 2019-08-16 18:50

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]
查看更多
登录 后发表回答