Htaccess help, $1 being used before its set??? what?

送分小仙女□ 提交于 2020-02-05 05:50:32

问题


I have this snippet

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|img|css|js|robots\.txt)
RewriteRule (.*) index.php?/$1 [L] 

It won't allow me to access a file at website.com/js/main.php but it will let me access index.php

According to my webhost, $1 is being called before it is set. Any solutions?

I'll accept answers when i get back tomorrow. Thank you!


回答1:


I'm assuming you want the rewrite to ignore the things which your condition currently specifies. In that case...

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond !^(index\.php|images|img|css|js|robots\.txt)
RewriteRule (.*) index.php?/$1 [L,QSA]

...should work fine. You'll probably want the QSA on there so that if there's a query string, it's properly handled.




回答2:


Your web host is wrong. The order of ruleset processing is:

  1. pattern in RewriteRule is tested (that will populate the $N references with values)
  2. associated RewriteCond conditions are tested (if present)

Only if the pattern matches the current URL path and the associated condition is fulfilled, the pattern is applied.

So in your case the pattern (.*) is tested on the current URL path js/main.php (without local prefix /). It matches ($0=js/main.php, $1=js/main.php) so the three associated conditions are tested in the order they appear:

  1. RewriteCond %{REQUEST_FILENAME} !-f
  2. RewriteCond %{REQUEST_FILENAME} !-d
  3. RewriteCond $1 !^(index\.php|images|img|css|js|robots\.txt)

Assuming that the requested URL path /js/main.php does not refer to an existing file or directory, the first conditions are both true. But the third one will evaluate to false as $1=js/main.php and the pattern ^(index\.php|images|img|css|js|robots\.txt) matches (^js branch) js/main.php. So the condition is not fulfilled and the rule is not applied.



来源:https://stackoverflow.com/questions/2377241/htaccess-help-1-being-used-before-its-set-what

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