Thomas Clayson
Thomas Clayson

Reputation: 29935

mod_rewrite and .htaccess - stop rewrite if script/file exists

This is my htaccess:

RewriteEngine On



# Stop if it's a request to an existing file.

RewriteCond %{REQUEST_FILENAME} -f [NC,OR]

RewriteCond %{REQUEST_FILENAME} -d [NC]

RewriteRule .* - [L]



# Redirect all requests to the index page

RewriteRule ^([^/])         /index.php          [L]

Now this forwards everything to my index.php script! It dosen't stop if a script exists. Anyone have any idea why this isn't working? I've looked everywhere, but I don't really understand mod_rewrite (as much as I thought!).

The problem has come about because I've put in <script> tags which point to .js files in my web directory which are then forwarded to the index.php script. The web developer toolbar tells me this. :) And clicking links to the js files in firefox's view source window also shows the index.php output.

thank you.

Upvotes: 6

Views: 8671

Answers (2)

John F
John F

Reputation: 735

I guess you should add [OR] after the first condition like this:

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1 [QSA,L]

Upvotes: 1

vbence
vbence

Reputation: 20333

This is because after processing a rewrite rule the whole process restarts with the new url. The processing of an url can go thru the rules over and over again each time with the changed URL, until there is no more change (no applying rules found).

You need this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)         /index.php          [L]

Don't think of the rules as a program, they are rules which can overlap and must be as specific as possible with each one.

Upvotes: 8

Related Questions