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 <em>that Apache module which tries to guess which file were you referring to when a URL was misspelled</em>, since even if I commented out the rule it still executed <strong>section.php</strong> when entering <em>/section</em> 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 <strong>mod_negotiation</strong>, 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.

<h3>Disabling mod_negotiation</h3>

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.

<h3>Curious cat strikes back</h3>

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 ;-)