Forum Moderators: phranque

Message Too Old, No Replies

The page seems to redirect to itself

infinite redirect

         

e11studios

10:00 pm on Jan 7, 2014 (gmt 0)

10+ Year Member



I believe this has something to do with the htaccess file, but I'm really in uncharted waters with this one.

I am getting the following error "The page seems to redirect to itself. This may result in an infinite redirect loop." for this page http://www.example.com/faqs

From my htaccess file:

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Use PHP 5.3.1
AddType application/x-httpd-php53 .php

RewriteEngine on
RewriteBase /

# add www to domain if missing
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

# Enable expirations
ExpiresActive On

# Default directive
ExpiresDefault "access plus 1 week"

# My favicon
ExpiresByType image/x-icon "access plus 1 year”

# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"

# CSS
ExpiresByType text/css "access 1 month”

# Javascript
ExpiresByType application/javascript "access plus 1 year"

RewriteEngine on
RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} Wget [OR]
RewriteCond %{HTTP_USER_AGENT} CherryPickerSE [OR]
RewriteCond %{HTTP_USER_AGENT} CherryPickerElite [OR]
RewriteCond %{HTTP_USER_AGENT} EmailCollector [OR]
RewriteCond %{HTTP_USER_AGENT} EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} EmailWolf [OR]
RewriteCond %{HTTP_USER_AGENT} ExtractorPro
RewriteRule ^.*$ deny.html [L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

AuthUserFile "/home/rdavis/.htpasswds/public_html/passwd"

Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 /404.html
ErrorDocument 500 http://www.example.com/index.html

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.net$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|mov|mp4)$ www.example.com [R,NC]



Does something pop out as being incorrect? I'm sorry, I get very confused with htaccess files.

[edited by: phranque at 8:46 am (utc) on Jan 8, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

lucy24

12:00 am on Jan 8, 2014 (gmt 0)

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



Oh, ###. Looks like you've got fragments of at least three and possibly as many as five different htaccess files in there.

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} Wget [OR]
RewriteCond %{HTTP_USER_AGENT} CherryPickerSE [OR]
<snip, snip>
RewriteCond %{HTTP_USER_AGENT} ExtractorPro
RewriteRule ^.*$ deny.html [L]

What the bleep? "Rewrite ALL requests from selected bad guys to (I assume) 403 page while letting them think they got what they asked for." I have to assume you're doing this intentionally. What's the backstory?

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^ <standard hotlink list here>
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|mov|mp4)$ www.episodexistudios.com [R,NC]


Huh what? squared. "If image request is from an unauthorized source, issue a temporary redirect to my front page". This one I honestly don't understand. Do you get a lot of humans clicking links on other people's sites saying "Look at this cool picture!" That's the only time the redirect would work as intended.

Never mind about diagnosing a specific problem. You need to get the whole htaccess file cleaned up.

g1smd

12:18 am on Jan 8, 2014 (gmt 0)

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



Gather up all of your blocks of RewriteRule code.

List the blocks of code in this order in the file:
- rules that block access
- rules that redirect (with the non-www to www redirect last in this group)
- rules that internally rewrite.

Number each of the rules (in the comment) so that it will make it easier to discuss them here.

You only need the RewriteEngine On directive once, likewise the Options. Move them to the beginning.

List all the other non-RewriteRule code (Add.., Expires.., Auth.., Error...) after all of that.

lucy24

7:48 am on Jan 8, 2014 (gmt 0)

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



List all the other non-RewriteRule code (Add.., Expires.., Auth.., Error...) after all of that.

Heh. Now, personally I like to put all that stuff first. There's not much of it, so it seems most sensible to collect it right at the top :) Probably for arcane historical reasons, my very first line is Options. Then things like ErrorDocuments. If I had mod_authz-thingy and mod_rewrite in the same htaccess, the Allow/Deny stuff would come before the RewriteRules.

phranque

8:55 am on Jan 8, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, e11studios!


does "fetch as googlebot" show a multiple redirect chain?
if not, this message is probably a bug in this feature of GWT.

e11studios

6:10 pm on Jan 8, 2014 (gmt 0)

10+ Year Member



Thank you all for your input. I'm afraid that this is really beyond my understanding. Perhaps someone could recommend where I may find some training on this file, so that I can start from scratch. Again, I don't know anything about it.

My goals are:
Make the search engines happy
Clean up the url's of wordpress and other php pages
Direct to my 404 page when not found
Redirect (with or without www) to episodexistudios.com
Redirect e11studios.com to episodexistudios.com

lucy24

7:52 pm on Jan 8, 2014 (gmt 0)

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



Start with g1smd's suggestion: collect everything having to do with mod_rewrite and pull it into one group. That means:

--all RewriteEngine On directives
--all RewriteBase statements
--all <IfModule evelopes, with their contents
--finally, all rulesets: the line RewriteRule blahblah plus any preceding RewriteCond blahblah lines

A number of these will turn out to be redundant. But the first step is to collect them all in one place. Fire up your favorite text editor and do the work on a copy of the file.

You've got one thing going for you: Right now there don't seem to be any rules using mod_alias (Redirect by that name). This is good, because it's one less thing to fix.

All this is assuming you're on shared hosting, so htaccess is a necessity.

e11studios

8:46 pm on Jan 8, 2014 (gmt 0)

10+ Year Member



RewriteEngine on
RewriteBase /
ErrorDocument 404 /404.html
ErrorDocument 500 http://www.example.com/index.html

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.net$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^example\.com [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Use PHP 5.3.1
AddType application/x-httpd-php53 .php

# Enable expirations
ExpiresActive On

# Default directive
ExpiresDefault "access plus 1 week"

# My favicon
ExpiresByType image/x-icon "access plus 1 year”

# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"

# CSS
ExpiresByType text/css "access 1 month”

# Javascript
ExpiresByType application/javascript "access plus 1 year"

[edited by: phranque at 11:05 pm (utc) on Jan 8, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

phranque

11:20 pm on Jan 8, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



RewriteBase /

you don't need RewriteBase in this case.


ErrorDocument 500 http://www.example.com/index.html

you should not specify the protocol and hostname for the ErrorDocument or the server will send a 302 status code and Location: http://www.example.com/index.html header instead of a 500 status code.
use this instead:
ErrorDocument 500 /index.html



<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
...
</IfModule>

- since you already know mod_rewrite is installed, you don't need the IfModule envelope.
- you already have a "RewriteEngine On"
- remove this instance of RewriteBase as well.


RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.net$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^example\.com [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

why are you testing HTTP_REFERER in those RewriteCond directives?
it seems that would leave you open to hostname canonicalization issues.
i think what you want is to use HTTP_HOST and consolidate all those conditions into one:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

lucy24

1:57 am on Jan 9, 2014 (gmt 0)

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



phranque, I think you over-exemplified ;) Look back at the first post and you'll see it's some type of anti-hotlinking function.

e11studios

10:46 pm on Jan 10, 2014 (gmt 0)

10+ Year Member



So this is what I have now:

RewriteEngine on
ErrorDocument 404 /404.html
ErrorDocument 500 /index.html

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Use PHP 5.3.1
AddType application/x-httpd-php53 .php

# Enable expirations
ExpiresActive On

# Default directive
ExpiresDefault "access plus 1 week"

# My favicon
ExpiresByType image/x-icon "access plus 1 year”

# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"

# CSS
ExpiresByType text/css "access 1 month”

# Javascript
ExpiresByType application/javascript "access plus 1 year"



Question: How should the redirect from one of my domains to the other be programed? Or did I remove something that I shouldn't have?

lucy24

1:57 am on Jan 11, 2014 (gmt 0)

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



You must have cut something.

:: detour to text editor ::

The htaccess file as originally posted was about 84 lines. Of those, 32 pertained to mod_rewrite. (It is easy to check, because all mod_rewrite directives happen to begin with the element Rewrite...) A quick cut-and-paste confirms that, yes indeed, there are five separate occurences of "RewriteEngine On", and two RewriteBase. Get rid of the RewriteBase altogether, and consolidate the others, and you're left with-- still in the original order, with added blank lines--

RewriteEngine on

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

# Rule 2
RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]

# Rule 3
RewriteCond %{HTTP_USER_AGENT} Wget [OR]
RewriteCond %{HTTP_USER_AGENT} CherryPickerSE [OR]
RewriteCond %{HTTP_USER_AGENT} CherryPickerElite [OR]
RewriteCond %{HTTP_USER_AGENT} EmailCollector [OR]
RewriteCond %{HTTP_USER_AGENT} EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} EmailWolf [OR]
RewriteCond %{HTTP_USER_AGENT} ExtractorPro
RewriteRule ^.*$ deny.html [L]

# Rule 4
RewriteRule ^index\.php$ - [L]

# Rule 5
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# Rule 6
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.net$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.net$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|mov|mp4)$ www.example.com [R,NC]

for a total of 26 lines, six rules. Collect them all and put them at the end of your htaccess file. Or at the beginning, if you insist; the main thing is to collect all lines beginning with "Rewrite..."

Necessary changes:

The original Rule 3 goes first. It's about access control. It isn't clear to me whether you are intentionally rewriting to the "deny" page while letting them think all is 200 hunky dory. But structurally it doesn't matter: no matter what flags are attached to the rule, it's "really" an access-control rule.

The original Rules 2 and 6 go next. They're concerned with specific filetypes. But the flags are wrong. At a minimum, each separate rule requires the [L] flag unless (a) it's got a flag such as [F] that carries an implied [L] or (b) you've got a specific and particular reason for leaving it out. I frankly don't understand Rule 6-- the hotlink package-- but that may be a subject for a different thread.

Then comes Rule 1. The domain-name-canonicalization redirect is always your very last redirect ([R=301,L] flag). But the Condition isn't optimal. If you don't have subdomains to filter out, the ordinary form is

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$

Immediately before Rule 1 is something you haven't currently got: the index.redirect. It looks something like this:

RewriteRule ^(([^/]+/)*)index\.(html?|php) http://www.example.com/$1 [R=301,L]

That leaves your original Rules 4 and 5, which stay together. And here's where we get into political/philosophical issues involving that whole -f and -d thing. At a minimum, constrain the rule to requests for pages. Your CMS is not handling image requests, is it? So something like

RewriteRule (^|/|\.php)$ /index.php [L]

If you don't do this, the server has to stop and check for a file's existence on every single request. Does this image exist? Yup, it does. Is this a real stylesheet? Guess so. And what if the file doesn't exist? That's a matter for a 404 response, not for the CMS getting fancy. (Obvious exeption if there are particular types of non-page requests that the CMS does handle.)

phranque

3:52 am on Jan 11, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



phranque, I think you over-exemplified. Look back at the first post and you'll see it's some type of anti-hotlinking function.

my response was based on e11studios's preceding post not the OP.