Tutorial :Mod Rewrite Skip Rules Server error



Question:

The following code gives me an internal server error and I cannot determine why.

RewriteEngine On  SetEnv APPLICATION production  RewriteCond %{ENV:APPLICATION} =production  RewriteRule ^.*$ - [S=1]   RewriteRule ^(.*)$ html/$1.html  

All it should do is take the url

/test  

and turn have it reference

/html/test.html  

behind the scenes, but only if the environment is production, which is set by the SetEnv


Solution:1

Maybe my mod rewrite is rusty, but if I understand it, that seems to be doing the opposite of what you are saying. If application is production, then it WON'T rewrite the url (and it skips the Rule that would). I'm not seeing anything that would cause an internal server error though.

To do what you are saying, you would need something like:

RewriteEngine On  SetEnv APPLICATION production  RewriteCond %{ENV:APPLICATION} !=production  RewriteRule ^.*$ - [S=1]   RewriteRule ^(.*)$ html/$1.html  

If you are still having problems, check your apache error log, that has the best info for Internal Server Error 500


Solution:2

Either do it like Todd Gardner said or remove the redundant rule and bind the condition to the second rule:

RewriteEngine On  RewriteCond %{ENV:APPLICATION} =production  RewriteRule ^(.*)$ html/$1.html  

Additionally I’d use this rule instead to avoid recursion:

RewriteCond %{ENV:APPLICATION} =production  RewriteRule !^html/ html%{REQUEST_URI}.html  


Solution:3

Turn on mod rewrite logging to get useful errors.

RewriteLog rewrite_log  ReWriteLogLevel 2  

Adjust the log level for verbosity.


Solution:4

You have to put quotes around the string to compare it correctly

RewriteCond %{ENV:APPLICATION} !="production"  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »