Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 716
Thomas Clayson Avatar asked Mar 14 '11 23:03

Thomas Clayson


2 Answers

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.

like image 151
vbence Avatar answered Dec 03 '22 14:12

vbence


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]
like image 42
John F Avatar answered Dec 03 '22 13:12

John F