tbeauvais
tbeauvais

Reputation: 1738

Why am I getting a 500 error with these RewriteRules

I am setting up a site on a shared hosting plan so I am stuck using Apache and a .htaccess file. I have 2 RewriteRules defined. Both rules work perfectly on a local machine running Apache.

The first rule is to rewrite requests for /css/*.css to /www/css/*.css The second rewrites everything else to /www/index.php.

The first rule regarding CSS/JS files is the one causing 500 errors but I can't figure out why. I have tried every different incarnation of these rules and always get a 500.

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.(css|js) www/$1.$2 [L]
RewriteRule ^(.*)$ www/index.php [L]

Upvotes: 0

Views: 901

Answers (1)

LazyOne
LazyOne

Reputation: 165298

Because you wrote your rules in such way that they create infinite rewrite loop which Apache has to break at some point, hence the 500 Internal Server Error. If you check your error log you will see exact error message.

The [L] flag does not necessary mean "rewrite done" -- it just means "rewrite done on this iteration -- lets go again from start".

Very useful to read: RewriteRule Last [L] flag not working?

To solve your problem -- you need to add some condition so that already rewritten rules are not get rewritten again and again. These rules should do the job for you (one of the possible solutions -- it all depends on how your actual project is set up):

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^(.*)\.(css|js) www/$1.$2 [L]

RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^(.*)$ www/index.php [L]

Upvotes: 1

Related Questions