Forum Moderators: phranque
http://www.example.com/a/
http://www.example.com/a/index.php
To redirect all requests for a/index.php to a/, I put the following lines in the a/.htaccess file:
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ /a/ [R=301,L]
On my local machine it works fine. However when I upload the file to the server, I receive the following error message in the browser:
"Firefox has detected that the server is redirecting the request for this address in a way that will never complete."
I guess this message means that there is an infinite loop of redirects, but how is that?! There is only 1 rewrite rule in the .htaccess file ... Also, I can not spot anything in the .htaccess in the parent directory that redirects 'a/' URL.
On my local apache it works fine. Is there anything in httpd.conf that may cause this kind of behavior?
Thanks
On my local apache it works fine
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ /a/ [R=301,L]
This is how I have it (just for the homepage, using .html extension) - using the full URL in the RewriteRule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]
But I do it just for the homepage, so I suppose there would be a difference and more would have to be coded (with an expression using a back-reference) for the RewriteRule to work with all subdirectories.
Added:
Aha! Here's where I probably swiped mine from originally, and there's a full explanation of why THE_REQUEST is used:
[webmasterworld.com...]
There's plenty more:
subdirectory redirect index.html [google.com]
[edited by: Marcia at 1:04 pm (utc) on April 11, 2008]
Many thanks, that solved my problem. I only changed the rule slightly to apply in the directory a/, but not in the sub-directories.
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^index\.php$ /a/ [R=301,L]
I guess using THE_REQUEST was the key. I still do not understand though "the loop" thing. How was /a/ redirected back to /a/index.php?
Thanks again
DirectoryIndex index.html index.htm index.php
So, your browser requested "/", DirectoryIndex rewrote it to "/index.php", and then your rewrite rule redirected the browser to "/" and the loop repeated.
By examining the browser request, your rewrite rule will redirect only if the client asked for "index.php" and not if the current URL is index.php as the result of a DirectoryIndex directive or an internal rewrite rule.
You can make the RewriteCond a bit more efficient by eliminating the ambiguous ".*" subpattern:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
Jim
RewriteEngine on
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]
Make sure that every page on your site can be reached with one and only one URL; All other URL variations should be redirected to the single, canonical URL.
Look for problems with:
Jim