This Apache setting breaks mod_rewrite

I have had this problem on two very different grades of Apache servers now (cheap, shared stuff and a high-end, expensive, HIPAA-compliant box), so I thought I’d post something reasonably search-friendly in case someone else was looking for the answer.

If you are trying to use mod_rewrite the way a normal human would, to rewrite things like host/users/1/ to host/users.php?id=1 or host/page/slug-for-page to host/page.php?slug=slug-for-page, and you have “MultiViews” turned on in Apache, they will not work. Here’s what that setting does, straight from the documentation:

If the server receives a request for /some/dir/foo and /some/dir/foo does not exist, then the server reads the directory looking for all files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client’s requirements, and returns that document.

That means the server is effectively guessing what file you’d want them to see if the one they ask for doesn’t exist, which sounds like something I (and damn near everyone else) would 100% never want to happen. I’m not sure why it seems to defaulted on (or alternatively, why so many hosts turn it on themselves). If a page is missing, I want a 404. I want it logged, and I want to know. Period.

If you are like me and would like this to never happen, you can turn it off via Apache config if you have access to that, or you can turn it off in .htaccess using Options -MultiViews.

About Scott Allen

Scott Allen is a web developer from Indianapolis, Indiana. He has been working with HTML, CSS, PHP, & MySQL since the late '90s, and has extensive experience in database design and development, server-side scripting, content management, and front-end user experience, especially in the creation of educational content. Connect with Scott on Twitter (preferably), or Facebook or Google+ (if you aren't in a hurry).
This entry was posted in Code, Tech and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *