Forum Moderators: phranque
[example.com...]
to
[example.com...]
using this in htaccess:
RewriteRule ^news\/(.*)\/(.*)$ /news/$1.php$2 [T=application/x-httpd-php,NC,L] But now I noticed that there are some links out there on external sites that still use the old .php How do I redirect them to the new pattern without getting caught in a loop?
This fails badly before or after the above line:
RewriteRule ^news\/(.*)\.php(.*)$ http://example.com/news/$1/$2 [NC,L,R] It loops continuously and I think I understand why, it's negating the previous line. I thought the "L" should stop that, no?
So how can I break that cycle and still tell bots/browser there is a redirect?
Because your second rule contains (and should contain) an [R] flag, this causes the server to issue a 302 redirect response back to the browser. The browser gets the new URL from that response and then issues a new HTTP request.
RonPK has addressed a possible cure, but I'd also like to suggest an improvement to your original rule as well:
RewriteRule ^news/([^/]*)/([^/]*)$ /news/$1.php$2 [T=application/x-httpd-php,NC,L]
Jim
However even with the condition it's still short circuiting between the two rules.
Apache simply doesn't know which rule to obey.
The url comes in as example.com/news/20040820.php
so the second (new) rule changes it to a redirect for example.com/news/20040820/
(simply to show properly in the visitor's browser or search engine index)
but then it goes through the rules again and hits the first rule which changes it back to the .php and the cycle starts over again!
From [httpd.apache.org...]
A MultiViews search is enabled by the MultiViews Option. 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.
I've mentioned Option MultiViews a time or two in the past but never got any reaction. I think it would make life easier for many people. It's one of the first things I add to a new site's .htaccess.
But everyone can and should make their own choices, so this is certainly an option here.
Jim