mod_rewrite, mod_negotiation and empty $_GET's

I had just added this simple rewrite rule into the .htaccess file


RewriteRule ^section/(\d+)/$ /section.php?id=$1 [L]

and went to section.php expecting to find the id value inside $_GET['id']. Big surprise when it was actually empty. I suspected this could be something to do with that Apache module which tries to guess which file were you referring to when a URL was misspelled, since even if I commented out the rule it still executed section.php when entering /section in the URL. But if the .php file was completely unrelated to the rewrite pattern, (e.g. with RewriteRule ^kkkkkkk/(\d+)$/ /blablabla.php?id=$1) it worked. Very odd!

Since I personally never needed that feature, I didn't know its name (although I have seen it in a couple of servers before). After a while and several google searches, I could confirm the guilty and the one to blame was mod_negotiation, precisely the module which tries to guess which file you really wanted.

I haven't investigated exactly what is going on inside apache, although I guess that basically mod_negotiation is executed before mod_rewrite has a chance to parse the request, and when mod_rewrite is called it receives an overwritten and modified version of $_GET which is completely useless at that point.

So there are two options for getting mod_negotiation out of our way: disabling it altogether or disabling it at .htaccess level.

Disabling mod_negotiation

You can only do this if you control the server, be it a dedicated server, a slice server, or a local computer. This is how I did it with Ubuntu (Hardy Heron):


sudo a2dismod negotiation
sudo /etc/init.d/apache2 force-reload

First line disables the module, second one forces apache to restart (obvious, but just in case someone got lost ;) )

Disabling at .htaccess level

If you're on a shared hosting or you need mod_negotiation for something else, there's still hope!

Just add this


Options -Multiviews

at the top of .htaccess and that's it.

Curious cat strikes back

And out of curiosity... does anybody enable mod_negotiation on purpose? I personally find it more of an annoyance than a feature, specially when one wants to show only certain stuff and discovers that Apache is showing more than it should thanks to this evil module. Anyway, enjoy mod_rewrite ;-)