htaccess rewrite not working for all rules
-
06-07-2019 - |
Question
This is a very strange problem, and I just hope that I can clearly explain it.
Basically, we made a major update on a client site today, and needed to update some rewrite rules in the htaccess to accomodate the new structure, etc...
So, where we originally had things like:
RewriteRule ^/resources/?$ index.php?id=resources
RewriteRule ^/media/?$ media.php
We changed to:
RewriteRule ^/resources/?$ index.php?id=resources
RewriteRule ^/media/?$ index.php?id=media
But when we visited http://www.example.com/media - it was displaying the old media page. Then, when we removed the old media.php from the document root, we got 404s. Something somewhere is still mapping /media to media.php - and ignoring the rewrite rule.
The weird thing is - all the other rewrite rules in the file are working fine - and there are about 20 or so in there.
We've tracked up and down the vhost and other config files, but can't find any reference to media or the other broken redirects (three in total) - so we're stumped.
Solution 2
Disabling the MultiViews option in the site .htaccess fixed the issue - it seemed that this was enabling content negotiation which was being processed before the rewrite rules.
OTHER TIPS
I strongly suspect this would be due to AcceptPathInfo, which in my view is an Apache mis-feature. By default I believe it is switched on where PHP is the handler.
Try adding to your .htaccess, or preferably your httpd.conf (and restart):
AcceptPathInfo Off
See: http://httpd.apache.org/docs/2.2/mod/core.html#acceptpathinfo
--
A few side points:
You should ensure that your RewriteRules always end with [L] when you have found a match - it will help in debugging. E.g.:
RewriteRule ^/media/?$ index.php?id=media [L]
Also, you could reduce the number of rules by combining similar ones; e.g.:
RewriteRule ^/(media|resources)/?$ index.php?id=$1 [L]
Personally I think the use of /? at the end of your first-half is not good, because it means that both URLs work, without redirection, meaning that you do not have a definitive one that is 'correct'. Personally I would use:
RewriteRule ^/(media|resources)/$ index.php?id=$1 [L]
RewriteRule ^/(media|resources)$ /$1/ [L,R]
In other words, a unique resource should have a single, canonical (definitive) URL.