Nrgyzer
Nrgyzer

Reputation: 1075

htaccess shows "Not Found" if directory does not exist

I want rewrite everything that matches <domain>/something/mypage.htm to something.php?pid=mypage, so I wrote the following .htaccess file:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).htm$ $1.php?pid=$2
DirectoryIndex home.php

On my old webhosting provider, the rules worked as expected, but on the new provider, it says "Not Found" when I try to open <domain>/something/index.htm or similar. But when I create a directory called something, the rewrite rule works as expected.

Is anyone having any idea what's wrong here?

Upvotes: 1

Views: 96

Answers (1)

MrWhite
MrWhite

Reputation: 45829

It looks like MultiViews is perhaps enabled on the new server and this will conflict with your mod_rewrite directive.

With MultiViews enabled and you request /something/mypage.htm (when /something.php exists as a physical file), mod_negotiation will issue a subrequest for /something.php/mypage.htm (that's /something.php with path-info of /mypage.htm) before your mod_rewrite directive is able to process the request, so no pid parameter is passed. (Your directive does not match a dot in the first path segment, so the subrequest by MultiViews does not match.)

The 404 will result from either Apache, if AcceptPathInfo Off is set (perhaps in the server config), or from your script because the pid parameter is not being passed.

When you create a subdirectory called something, MultiViews does not rewrite the request since it already matches something, ie. the directory by that name.

You need to ensure that MultiViews is disabled at the top of your .htaccess file:

Options -MultiViews

NB: MultiViews is not enabled by default on Apache, but some shared webhosts do enable it for some reason. It makes extensionless URLs "magically" work out of the box, but actually causes many more problems if you are not expecting it.

Upvotes: 1

Related Questions