htaccess rewrite doesn't work when path has same name as file

780 Views Asked by At

I have a strange problem. Here are two examples of a URL rewrite in my htaccess file. The first one doesn't work and returns the variable for "query" as null in the PHP file.

RewriteRule ^search/([A-Za-z0-9-\s]+)$ search.php?query=$1

The second one (that I have used as a test) works perfectly fine.

RewriteRule ^foo/([A-Za-z0-9-\s]+)$ search.php?query=$1

I have found that the problem is that the beginning of the pattern cannot be the same as the name of the php file I'm using with it. This has always worked for me, and I am only now encountering this problem as I have moved to a new server.

Is there some kind of setting that is causing this?

1

There are 1 best solutions below

1
On BEST ANSWER

You need to place this line on top of your .htaccess to turn off MultiViews option:

Options -MultiViews

Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.