Forum Moderators: phranque

Message Too Old, No Replies

endless loop when 301-ing index.html

         

the_nerd

8:27 am on Nov 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried to permantenly redirect mydom.tld/index.html to mydom.tld but it resulted in some kind of loop. Redirecting works fine for all other pages, just index.html brings the problem.

My provider said the problem could be that the file is redirected to itself. Any idea?

nerd.

jdMorgan

2:49 pm on Nov 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This subject has been covered here many times, but here it is once more:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]

This redirects *only* if the original client (i.e. browser or robot) request is for "/index.html", and will not redirect as a result of the action of the DirectoryIndex directive rewriting requests for "/" back to "/index.html", which is the likely cause of your loop as noted by your host. It will work at any directory level.

Jim

the_nerd

7:18 pm on Nov 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jim! Works perfectly.

I'd love to understand how the condition is constructed - in case the original thread is still somewhere, could you pls point my nose to it?

Thanks again

the_nerd

jdMorgan

7:39 pm on Nov 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The pattern for THE_REQUEST matches the entire request header, as originally sent by the client (browser). For example:

GET /index.html HTTP/1.1

or

PROPFIND /widgets.html HTTP/1.0

By checking the request header as originally sent by the browser, we avoid doing this redirect as a result of DirectoryIndex internally rewriting a client request for the "correct" URL-path "/" to the "correct" filepath "/index.html", which would then result in another invocation of the rewrite rule, another DirectoryIndex rewrite, another rule invocation, etc. -- an 'infinite' loop.

In case it isn't obvious from the description above, Apache runs and re-runs the URL-manipulation and access-checking directives in .htaccess until no more internal rewrites are invoked as a result. Therefore, you should view .htaccess as repeatedly "calling itself" until all URLs are finally resolved to a file that exists.

Note that the [L] flag on a RewriteRule only stops mod_rewrite processing for the current pass through the mod_rewrite code. It does not force an exit from the URL-to-filename translation phase of the API to the content-handling API phase; This exit only takes place after Apache detects that no rewrites of any kind were invoked in the previously-completed pass, and that the result does not violate any access restrictions or point to a file that does not exist.

Jim

[edited by: jdMorgan at 7:50 pm (utc) on Nov. 17, 2006]

floriniri

6:52 am on Nov 18, 2006 (gmt 0)

10+ Year Member



you guys are great, just solved what I was dealing with. One question though: does it work if the index page is a php one if I replace html with php?

jdMorgan

2:35 pm on Nov 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest you try it instead of waiting for an answer here, especially on the weekend!

You can always test with a "nonsense" original URL like "outdox.php", request that URL from your server, and make sure it redirects to the proper URL. In this way, you won't interfere with the operation of your site, and can gain some confidence that it will work. Having reassured yourself, you can then change the URL pattern to "index\.php" and re-test.

Be sure to replace both instances of the URL pattern, and escape the literal periods as shown.

Jim