Apache ignoring RewriteRule when no extension in filename
Posted June 16th, 2011 in Apache
In the process of moving a LAMP web application from its old sever (with CentOS 5) to a brand newly installed server (with Debian 6) I found I was having a weird issue with Apache.
I had a rewrite rule so e.g. foo/1/2/3 would rewrite to foo.php?a=1&b=2&c=3 but the rewrite rules didn't appear to be working although the foo.php file was being served, despite the fact there was no extension present in the URL and there was a bunch of other stuff after the filename.
The culprint: MultiViews
It turned out that while I was setting everything up, either I'd copied over, or left as-is in the default Apache install the following setting:
<Directory /var/www>
Options -Indexes FollowSymLinks MultiViews
</Directory>
What MultiViews does, if it can't find the file named "foo", it then looks for any file named "foo" with an extension. In my case, it was then finding "foo.php" and serving that, bypassing my RewriteRules.
The solution was to simply remove MultiViews, so it now looks like this:
<Directory /var/www>
Options -Indexes FollowSymLinks
</Directory>
Then restart Apache and the rewrite rules will work again. I am a little surprised that MultiViews ignores a RewriteRule; perhaps there's some other setting to make them play nicely. In any case, I'd personally prefer to have MultiViews disabled.
Related posts:
- RewriteRule redirect without the query string (Friday, February 22nd 2013)
- Howto Restart Apache (Wednesday, December 22nd 2004)

Comments
blog comments powered by Disqus