I have a RewriteRule
in a .htaccess
file that isn't doing anything. How do I troubleshoot this?
.htaccess
file is even being read and obeyed by Apache? Can I write an echo "it is working" message, if I do write it, where would that line be echoed out?.htaccess
file isn't being used, how can I make Apache use it?.htaccess
is being used but my RewriteRule
still isn't having an effect, what more can I do to debug?This question is related to
apache
.htaccess
mod-rewrite
Generally any change in the .htaccess should have visible effects. If no effect, check your configuration apache files, something like:
<Directory ..>
...
AllowOverride None
...
</Directory>
Should be changed to
AllowOverride All
And you'll be able to change directives in .htaccess files.
To test your htaccess rewrite rules, simply fill in the url that you're applying the rules to, place the contents of your htaccess on the larger input area and press "Test" button.
The 'Enter some junk value' answer didn't do the trick for me, my site was continuing to load despite the entered junk.
Instead I added the following line to the top of the .htaccess file:
deny from all
This will quickly let you know if .htaccess is being picked up or not. If the .htaccess is being used, the files in that folder won't load at all.
To answer the first question of the three asked, a simple way to see if the .htaccess file is working or not is to trigger a custom error at the top of the .htaccess file:
ErrorDocument 200 "Hello. This is your .htaccess file talking."
RewriteRule ^ - [L,R=200]
On to your second question, if the .htaccess file is not being read it is possible that the server's main Apache configuration has AllowOverride
set to None
. Apache's documentation has troubleshooting tips for that and other cases that may be preventing the .htaccess from taking effect.
Finally, to answer your third question, if you need to debug specific variables you are referencing in your rewrite rule or are using an expression that you want to evaluate independently of the rule you can do the following:
Output the variable you are referencing to make sure it has the value you are expecting:
ErrorDocument 200 "Request: %{THE_REQUEST} Referrer: %{HTTP_REFERER} Host: %{HTTP_HOST}"
RewriteRule ^ - [L,R=200]
Test the expression independently by putting it in an <If>
Directive. This allows you to make sure your expression is written properly or matching when you expect it to:
<If "%{REQUEST_URI} =~ /word$/">
ErrorDocument 200 "Your expression is priceless!"
RewriteRule ^ - [L,R=200]
</If>
Happy .htaccess debugging!
If you have access to apache bin directory you can use,
httpd -M
to check loaded modules first.
info_module (shared) isapi_module (shared) log_config_module (shared) cache_disk_module (shared) mime_module (shared) negotiation_module (shared) proxy_module (shared) proxy_ajp_module (shared) rewrite_module (shared) setenvif_module (shared) socache_shmcb_module (shared) ssl_module (shared) status_module (shared) version_module (shared) php5_module (shared)
After that simple directives like Options -Indexes
or deny from all
will solidify that .htaccess are working correctly.
Perhaps a more logical method would be to create a file (e.g. test.html), add some content and then try to set it as the index page:
DirectoryIndex test.html
For the most part, the .htaccess rule will override the Apache configuration where working at the directory/file level
Why not put some junk in your .htaccess file and try to reload apache. If apache fails to start you know its working. Remove the junk then reload apache if it loads congrats you configured .htaccess correctly.
Source: Stackoverflow.com