207

I have two main problems with mod_rewrite:

  1. There is no meaningful error reported when I have an invalid rule

    Enter image description here

  2. To reliably test each modification, I have to erase Google Chrome's cache. This isn't rocket science, but I have to hit Ctrl + Shift + Delete, click OK, and close the window, and reload.

I'd like to see if any of the gurus are willing to share their secrets to efficiently managing mod_rewrite code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
puk
  • 16,318
  • 29
  • 119
  • 199
  • 1
    Puk, see http://stackoverflow.com/questions/9153262/tips-for-debugging-htaccess-rewrite-rules where I discuss some of the stanbdard tricks. – TerryE Mar 10 '12 at 16:11
  • possible duplicate of [How to debug htaccess rewrite script](http://stackoverflow.com/questions/7738170/how-to-debug-htaccess-rewrite-script) – user Feb 21 '14 at 01:14

5 Answers5

311

One trick is to turn on the rewrite log. To turn it on, try this line in your Apache HTTP Server main configuration or current virtual host file (not in .htaccess):

LogLevel alert rewrite:trace6

Before Apache httpd 2.4 mod_rewrite, such a per-module logging configuration did not exist yet. Instead you could use the following logging settings:

RewriteEngine On
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ben
  • 4,392
  • 3
  • 23
  • 20
  • 1
    thanks. It's not what I was looking for, but still better than nothing. – puk Mar 09 '12 at 11:16
  • 84
    You cannot put this in .htaccess. You have to put it in the VirtualHost configuration. – Attila Szeremi Mar 14 '13 at 14:14
  • 2
    You cannot do this in `.htaccess`. `RewriteLog` and `RewriteLogLevel` are available only in server config and virtual host context – freedev Sep 20 '13 at 07:21
  • 8
    You have to have the `RewriteEngine On` section there too because if you only enable it (as i did) in the `.htaccess` file, then nothing will be logged. – chacham15 Nov 13 '13 at 23:20
  • 16
    where is the log file located in apache 2.4? – CJT3 Jan 26 '15 at 19:53
  • 8
    You'll find the 2.4 log items in the relevant error log. This may depend on your configuration, but default Debian/Ubuntu setups have them in /var/log/apache2/error.log – Josiah May 08 '15 at 21:34
  • 13
    Pro tip: Remember to turn your rewrite logging off. If you forget you'll fill your hard disk up fairly promptly, especially on a production server. – John Hunt May 21 '15 at 15:18
  • @chacham15, I do that all the time, put `RewriteLog` in a config file but don't set `RewriteEngine on` until the `.htaccess` file. Always works for me. – Octopus Jun 20 '17 at 21:06
  • 2
    This is not working in Apache 2.4. Before start wasting time, check Tobias Schultze answer below. – Tahi Reu Dec 08 '21 at 13:52
156

The RewriteLog directive as mentioned by Ben is not available any more in Apache 2.4.

You need to use the LogLevel directive instead. E.g.,

LogLevel alert rewrite:trace6

See Apache Module mod_rewrite, Logging.

Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120
Tobias Schultze
  • 1,670
  • 1
  • 10
  • 5
30

For basic URL resolution, use a command line fetcher like wget or curl to do the testing, rather than a manual browser. Then you don't have to clear any cache; just up arrow and press Enter in a shell to rerun your test fetches.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kaz
  • 55,781
  • 9
  • 100
  • 149
  • 14
    Another trick is to use Chrome "Porn mode" (Ctl+Shift+N). When you close the window, any cached session context is trashed. – TerryE Mar 10 '12 at 16:09
  • I think Firefox's "private session" browsing also. But are you saying this this context is per individual window (so you're not closing Chrome?) – Kaz Mar 11 '12 at 00:08
  • 2
    AFAIK, Chrome and Ff are different in that Ff runs as a single process which is in private mode or not. With Chrome, each tab or window runs as a separate process and can be individually in private mode; close a private window/tab and it context is trashed. – TerryE Mar 11 '12 at 00:48
  • 2
    PrivateTab addon for FF does the thing. Each tab works individually. – Javid Feb 11 '14 at 14:37
  • 1
    This method also shows redirects! Nice for someone that doesn't have access to the machine apache config file. – Geof Sawaya Jan 25 '17 at 06:12
25

There's the htaccess tester.

It shows which conditions were tested for a certain URL, which ones met the criteria and which rules got executed.

It seems to have some glitches, though.

Andy
  • 4,783
  • 2
  • 26
  • 51
  • 4
    For me it showed a green executed rule in the last line without any code. – Andy Jun 15 '15 at 12:50
  • @thombr can you be more precise, please? The link does not work? Or the tool? What exactly doesn't work? And why is that relevant in context of this question? – Andy Mar 09 '16 at 15:22
  • 3
    This tells me that the URL is being transformed as expected... however on the actual server I am getting a 404 – Michael Aug 02 '17 at 04:09
  • @michael is the tested rule the only one present in your configuration? Is mod_rewrite actually installed and active? – Andy Aug 02 '17 at 11:30
  • Turns out overrides were not enabled – Michael Aug 02 '17 at 17:44
3

Based on Ben's answer, you could do the following when running Apache on Linux (Debian in my case).

First create the file rewrite-log.load.

/etc/apache2/mods-availabe/rewrite-log.load

RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

Then enter

$ a2enmod rewrite-log

followed by

$ service apache2 restart

And when you are finished with debugging your rewrite rules,

$ a2dismod rewrite-log && service apache2 restart

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Flow
  • 23,572
  • 15
  • 99
  • 156
  • This did not work. I get sandino@envy:~$ sudo service apache2 restart * Restarting web server apache2 [fail] * The apache2 configtest failed. Output of config test was: AH00526: Syntax error on line 1 of /etc/apache2/mods-enabled/rewrite-log.load: Invalid command 'RewriteLog', perhaps misspelled or defined by a module not included in the server configuration – sandino Aug 24 '14 at 00:14
  • 4
    @sandino, what apache version are you running? It seems this syntax was changed for 2.4, use instead: `LogLevel warn rewrite:trace8` or `LogLevel info rewrite:trace8` where 8 can be any number from 1-8 – insaner Sep 13 '14 at 13:34
  • AH00526: Syntax error on line 1 of /etc/apache2/mods-enabled/rewrite Invalid command 'RewriteLog' – AnnoyinC Sep 20 '21 at 11:09