Forum Moderators: phranque

Message Too Old, No Replies

Redirect loop - what causes it?

Can't figure out why my htaccess file loops

         

zapotex

3:01 pm on Nov 30, 2011 (gmt 0)

10+ Year Member



Hi all,

I'm working on the suggestions I received in my previous posts and I got in trouble. This file gives me a redirect loop! Here is the code:

ErrorDocument 401 /errorpages/error-401.php
ErrorDocument 404 /errorpages/error-404.php
ErrorDocument 500 /errorpages/error-500.php

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteRule ^index\.html?$ / [NC,R=301,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

###I STARTED SEEING THE REDIRECT LOOP WHEN I INTRODUCED THIS CONDITION####
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteRule ^(.*) http://www.example.com/? [L,R=301]
###I STARTED SEEING THE REDIRECT LOOP WHEN I INTRODUCED THIS CONDITION####

RewriteCond %{REQUEST_URI} /galleries/$
RewriteRule ^(.*) http://www.example.com/galleries/index.php [L,R=301]

RewriteCond %{QUERY_STRING} ^cat=0$
RewriteCond %{REQUEST_URI} /galleries/index.php$
RewriteRule ^(.*) http://www.example.com/galleries/index.php? [L,R=301]


Can anybody help me? I really can't see any loops in this file.

Thanks a lot!

[edited by: eelixduppy at 1:29 am (utc) on Dec 1, 2011]
[edit reason] exemplified [/edit]

wilderness

3:45 pm on Nov 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm working on the suggestions I received in my previous posts


Apparently NOT?

The end of the first reply reads:

Use example.com in this forum. It stops the URL auto-linking process.


Please read the forum charter [webmasterworld.com]

zapotex

4:06 pm on Nov 30, 2011 (gmt 0)

10+ Year Member



HI wilderness,

sorry that I violated the rules. I checked whether the auto-linking process had happened and, when I saw it had not, I thought the post was compliant. I would edit the message now, but I get a message saying the time when I'm allowed to edit is over.

g1smd

8:14 pm on Nov 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The index redirect RewriteCond needs to test THE_REQUEST to avoid the redirect loop.

Redirects should be listed in order from most specific to most general.

The non-www/www redirect must be last.

Literal periods in patterns must be escaped.

For redirects the target should contain the protocol and domain name.

Never redirect to a named index file. The correct URL ends with a trailing slash.

zapotex

11:09 pm on Nov 30, 2011 (gmt 0)

10+ Year Member



Thank you so much for your reply!
Never redirect to a named index file. The correct URL ends with a trailing slash.

That's exactly what I was trying to do in the root folder! But I messed up, then I left the galleries/ folder as it was, until I understand how to do it correctly.
Redirects should be listed in order from most specific to most general.

The non-www/www redirect must be last.

Thanks a lot! I will edit it accordingly!
Literal periods in patterns must be escaped.

I forgot about it in one of the conditions... But it works the same (of course, . will match any character). I will correct it as you suggest!
The index redirect RewriteCond needs to test THE_REQUEST to avoid the redirect loop.

I'll google THE_REQUEST and learn how to use it! Thank you so much for the tip!

Thanks again for your answer! Best

lucy24

11:35 pm on Nov 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'll google THE_REQUEST and learn how to use it!

If you haven't already done so, bookmark this page:

[httpd.apache.org...]

It's listed in the Forums charter, but innocuously buried among a bunch of other links. It ought to be printed in red, twice as big as all the others ;)

zapotex

11:24 am on Dec 1, 2011 (gmt 0)

10+ Year Member



Ok, no more excuses, time to learn :-)

Thanks again!

zapotex

2:17 pm on Dec 1, 2011 (gmt 0)

10+ Year Member



The index redirect RewriteCond needs to test THE_REQUEST to avoid the redirect loop.

You know, I still don't get why using the REQUEST_URI variable causes a loop. I don't understand what in the new address matches any pattern that I have in my rules. But I'll try with THE_REQUEST and see if I ca fix it. Thanks so much!

g1smd

2:24 pm on Dec 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You request http://www.example.com/index.php and the internal pointer is set by your rule to point to http://www.example.com/ and this data is then sent back to the browser with the 301 status code.

You then request http://example.com/ and the internal REQUEST_URI file pointer is updated (rewritten) by Apache to point to index.php so that the file can be fetched.

Mod_rewrite runs again until all rules are satisfied.

So, with this next pass through the list of rules and with the REQUEST_URI pointer set to index.php, the pointer now matches your index.php ruleset and the request is redirected again.

If you test THE_REQUEST it will instead see GET / HTTP/1.1 and this will not match a test for GET /index.php HTTP/1.1 and the redirect will not occur.

[edited by: g1smd at 2:56 pm (utc) on Dec 1, 2011]

zapotex

2:54 pm on Dec 1, 2011 (gmt 0)

10+ Year Member



I tested it and it works with THE_REQUEST!

I must say that REQUEST_URI's behavior is quite perverse... It looks like it was designed on purpose to fool beginners :-)

Anyway, thanks to you now it works.

I'll use THE_REQUEST whenever at all possible from now on. At least it does not change in the middle of the execution!

Thanks so much!

lucy24

8:08 pm on Dec 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You then request http://example.com/ and the internal REQUEST_URI file pointer is updated (rewritten) by Apache to point to index.php so that the file can be fetched.


:: head spinning ::

So... REQUEST_URI includes the results of later Apache activity such as mod_index (and possibly mod_speling if you are rash enough to include it), while THE_REQUEST only looks at requests that came in from outside-- including 301/302 redirects.

Have I got that straight?

Are we all better off not thinking about REQUEST_FILENAME and PATH_INFO just yet?