Htaccess help, $1 being used before its set??? what?
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 开发者_StackOverflow社区is being called before it is set. Any solutions?
I'll accept answers when i get back tomorrow. Thank you!
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.
Your web host is wrong. The order of ruleset processing is:
- pattern in
RewriteRule
is tested (that will populate the$N
references with values) - 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:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
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.
精彩评论