jiexi
jiexi

Reputation: 3029

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 is being called before it is set. Any solutions?

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

Upvotes: 1

Views: 3198

Answers (2)

Gumbo
Gumbo

Reputation: 655269

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.

Upvotes: 0

Amber
Amber

Reputation: 526633

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.

Upvotes: 1

Related Questions