126

I have a RewriteRule in a .htaccess file that isn't doing anything. How do I troubleshoot this?

  • How can I verify if the .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?
  • If the .htaccess file isn't being used, how can I make Apache use it?
  • If the .htaccess is being used but my RewriteRule still isn't having an effect, what more can I do to debug?
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
macha
  • 7,337
  • 19
  • 62
  • 84
  • If you want to verify whether the htaccess file is being read by your webserver, just create a simple rule for testing .. learn more on this article https://helponnet.com/2021/05/14/know-wether-htaccess-is-working-on-apache/ – Amit Verma Sep 09 '21 at 00:14

9 Answers9

154

Enter some junk value into your .htaccess e.g. foo bar, sakjnaskljdnas any keyword not recognized by htaccess and visit your URL. If it is working, you should get a

500 Internal Server Error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request....

I suggest you to put it soon after RewriteEngine on.


Since you are on your machine. I presume you have access to apache .conf file.

open the .conf file, and look for a line similar to:

LoadModule rewrite_module modules/mod_rewrite.so

If it is commented(#), uncomment and restart apache.


To log rewrite

RewriteEngine On
RewriteLog "/path/to/rewrite.log"
RewriteLogLevel 9

Put the above 3 lines in your virtualhost. restart the httpd.

RewriteLogLevel 9 Using a high value for Level will slow down your Apache server dramatically! Use the rewriting logfile at a Level greater than 2 only for debugging! Level 9 will log almost every rewritelog detail.


UPDATE

Things have changed in Apache 2.4:

FROM Upgrading to 2.4 from 2.2

The RewriteLog and RewriteLogLevel directives have been removed. This functionality is now provided by configuring the appropriate level of logging for the mod_rewrite module using the LogLevel directive. See also the mod_rewrite logging section.

For more on LogLevel, refer LogLevel Directive

you can accomplish

RewriteLog "/path/to/rewrite.log"

in this manner now

LogLevel debug rewrite_module:debug
ThinkingMonkey
  • 12,539
  • 13
  • 57
  • 81
  • Ok, so the problem is, I am trying to add a rewriterule to the .htaccess file, and for some reason it is not using it. – macha Feb 10 '12 at 20:27
  • @macha try the above method. If you get an internal server error, that means mod_rewrite(.htaccess) is enabled. Are you on a shared host? – ThinkingMonkey Feb 10 '12 at 20:29
  • No I am working on my localhost, do you have an idea of when does the .htaccess file load? I though apache would read this first – macha Feb 10 '12 at 20:31
  • @macha .htaccess files never load. They are accessed(if present) by apache when you try to access a page which is present in that folder. – ThinkingMonkey Feb 10 '12 at 20:34
  • ok my question was, if somebody requests for a webpage, does apache directly goto the server side scripts or would it look for the .htaccess file in that folder and then proceed?? – macha Feb 10 '12 at 20:35
  • @macha apache will look for .htaccess files first process any rules then proceed to serve the page. Also, check for an update in answer. – ThinkingMonkey Feb 10 '12 at 20:38
  • Yeah, i did check if mod_rewrite.c exists, and did the RewriteEngine On. What I want to check is if apache is checking for this rule, so is there a way to log?? – macha Feb 10 '12 at 20:39
  • @ThinkingMonkey .. i tried all the options u mentioned.. plus checked the permissions of .htaccess, it is ASCII and not binary file , but still apparently apache still ignores the file.. whatever i write in the file it doesn't give any error or do anything.. any pointers ?? – Kamal Dec 05 '12 at 05:56
  • @Kamal are you sure you have enabled mod-rewrite? – ThinkingMonkey Dec 05 '12 at 12:08
  • Note: when setting up .htaccess files,check every tag in the httpd.conf file. Make sure the right directory has AllowOverride enabled. or [allowoverride all]. If this is not enabled, then the .htaccess file will be ignored. – E Net Arch Jul 20 '16 at 02:31
  • My site continues to load, even with "junk values" entered in. The `deny from all` answer by @caitriona did work (my site didn't pull up, verifying that `.htaccess` is being processed). FWIW, I'm using Litespeed, an Apache drop-in replacement. – rinogo Nov 29 '16 at 22:38
57

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.

caitriona
  • 8,569
  • 4
  • 32
  • 36
35

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.

eleuteron
  • 1,863
  • 20
  • 26
10

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!

PeterA
  • 775
  • 9
  • 15
8

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

cEz
  • 4,932
  • 1
  • 25
  • 38
3

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.

Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
1

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.

http://htaccess.mwl.be/

Ganesh Kandu
  • 601
  • 5
  • 15
0

Check whether the permission to read and execute the .htaccess by apache process is possible. Looking into the error_log of Apache will help you to sort the permission related error.

sathish V
  • 1
  • 2
-1

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.

Ben Rabidou
  • 355
  • 2
  • 5
  • 7
    I don't think Apache will fail to start or run if there is a bad .htaccess, given that it does not check .htaccess until there is a page request. – Andrew Feb 10 '12 at 21:01