Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect Error : Too Many Redirects

         

anand84

3:52 am on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

So far, I have had two websites 1 and 2 where No.2 pointed to a folder inside No.1. Now, I wanted to 301 redirect the No.2 website so that it is seen as a folder of No.1 only.

So, I added the following text in the .htaccess file of the No.2 website


Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://example.com/no2/$1 [R=301,L]


However, I am shown the following error message


This webpage has a redirect loop.

The webpage at http://example.com/no2/ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.

Here are some suggestions:
Reload this web page later.
Learn more about this problem.
More information on this error


I am not sure how to resolve this. Any ideas?

Thanks,

jdMorgan

1:43 pm on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mod_rewrite execution in .htaccess is recursive. That is, after any rule matches and is applied, mod_rewrite processing is re-started to see if any further rules will apply to this request.

Because you have used a non-specific ".*" pattern in your rule, the rule's output matches this pattern, so the request will be repeatedly redirected, first to /no2, and then to /no2/no2, and then to /no2/no2/no2, etc.

Use a negative-match RewriteCond to exclude the "/no2" path from being redirected, as in

RewriteCond $1 !^no2/

-or-
RewriteCond %{REQUEST_URI} !^/no2/

These constructs function identically, except that in .htaccess or in a server config file enclosed in a <Directory> container, the first will not 'see' a leading slash on the requested URL-path in $1, while the leading slash will always be present on REQUEST_URI.

Jim

anand84

6:26 pm on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello jdMorgan

Thanks a lot for your helpful post. I am pretty bad at understanding the technical aspects. So kindly bear with me. I have a few queries

1. I am assuming that http://example.com is compensated by the !^ characters. In my case since all these 301 redirect commands are issued inside the HTACCESS file, do you suggest me to use the REQUEST_URI line over the other option?

2. Where do I insert the ReWriteCond statement (before or after the following code)?


Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://example.com/no2/$1 [R=301,L]


Thanks,

jdMorgan

1:06 pm on Apr 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteConds go above the rule they are to be applied to. As stated, the two RewriteConds shown above function identically in the .htaccess context.

I would suggest that some time spent reading the resources cited in our Apache Forum Charter, reviewing the example threads and tutorials in our Apache Forum Library, and perhaps searching this site for prior examples would be a good investment. .htaccess is a server configuration file, and a single typo or small logical error in your code can take down your server immediately. Or worse, it can quietly ruin your search engine rankings over a period of time...

.htaccess code --and especially mod_rewrite code-- is not a copy-and-paste proposition.

Links to these resources are at the top of this screen.

Jim

jdMorgan

8:45 pm on Apr 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Where do I insert the ReWriteCond statement (before or after the following code)?

Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
#
RewriteCond $1 !^no2/
RewriteRule ^(.*)$ http://example.com/no2/$1 [R=301,L]

Jim

anand84

1:34 pm on Apr 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Jim

I tried this and was still noticing the same error. I shall be trying a few alterations and will update my experience by tomorrow.

Thanks for your help.

jdMorgan

1:51 pm on Apr 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Be sure to delete your browser cache before testing any changes to the code. Otherwise, your browser will show you previously-cached pages and response headers (including redirects), invalidating your test results.

If the code above still shows the same problem, then either your description of the locations of the .htaccess file is incorrect, the description of your URLs is not quite right, you have other code that is interfering, your server configuration is non-standard or incorrect, or you've introduced a typographical error.

The code is very simple: "If the requested URL-path does not already start with 'no2/', then add 'no2/' to the beginning of the URL-path and redirect the client to that new URL."

Jim

anand84

2:09 pm on Apr 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim,

I think there is a logical fallacy here. Please correct me if I'm wrong.

"If the requested URL-path does not already start with 'no2/', then add 'no2/' to the beginning of the URL-path and redirect the client to that new URL."


What I am attempting to do is redirect
example2.com
to
http://example.com/no2
. Wouldn't the code that you have provided mean I shall be adding an extra "no2" to my destination URL?

Thanks,
Anand

PS : Yes, I remember to delete browser cookies each time. Learnt it the hard way :-)

g1smd

2:37 pm on Apr 23, 2010 (gmt 0)

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



Mod_Rewrite works on URL requests.

Specifically it looks at the GET request sent by the browser.

The rule says look at the request, and if the path part does not begin with 'no2', update the value in the "URL" variable by adding 'no2' to the beginning of the path, and then send two response headers back to the browser: one saying "301 Moved Permanently" and the other containing this new URL value.

The browser then makes a new HTTP request for this new URL, and mod_Rewrite processing begins again by looking at the newly requested URL value.

So, quite simply, this code redirects users requesting a URL without 'no2', to a new URL that does include 'no2'. That's what a redirect does.


If what you wanted to be able to do, was to request a URL without 'no2' in it, and get served content from a folder called 'no2' located inside the server but without 'no2' appearing in the URL, then you should have asked for a 'rewrite'. A rewrite does a different thing to a 'redirect' even though the code is very similar. Rewrites are described in [webmasterworld.com...] for example.

anand84

10:48 am on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks g1smd. I think I have understood the basic reason for my error.

1. My website is located as a subfolder 'no2' of example.com
2. The domain name example1.com is pointing to this folder
3. Now, to offer a 301 redirect, I make changes to the htaccess file at example.com/no2 so that example1.com redirects to example.com/no2
4. So when I type example1.com in the address bar, it reads the htaccess file located at example.com/no2 which is asking to be redirected to example.com/no2. So now the URL is redirected to example.com/no2 which is again asking to be redirected to the same URL (since the htaccess file resides at the same address)

So, what I want to tell the server is that redirect once to wherever I ask it to and then stop. Do not redirect again.

Or, another solution will be to use some kind of an IF-Else loop where I can specify if the source url is example1.com, then redirect else do not redirect.

Is such a thing possible inside the HTACCESS file?

anand84

10:53 am on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think this is the rule:
RewriteRule ^/(string1|string2|string3)$ http://example.com/$1

I shall check and update you on the same.

Update : Doesn't seem to be working..Basically I had changed the (.*) parameter as follows

RewriteRule ^example1.com$ http://example.com/no2/$1

[edited by: anand84 at 11:08 am (utc) on Apr 24, 2010]

g1smd

11:03 am on Apr 24, 2010 (gmt 0)

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



Add a RewriteCond that checks what HTTP_HOST was requested.

Make sure it caters for both www and non-www requests.

anand84

11:25 am on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks g1smd.

I made the changes and this is how my latest htaccess file looks


Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
#
rewritecond %{http_host} ^example1.com
RewriteRule ^(.*)$ http://example.com/no2/$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress


So, when I type example1.com, it redirects to example.com/no2. However, it shows a 404 error (in the example1.com web design template). When I visit one of the inner pages of example1.com, I am redirected to the appropriate example.com/no2/innerpage. However, here I see a 404 page corresponding to the example.com web design.

So, the problem does not seem to be in the actual redirection, but with interpreting the corresponding content from the database..

anand84

12:04 pm on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I checked out an old thread from here which seems to very much concur with my issue : [webmasterworld.com...]

When I tried using the following code


Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^(www\.)?(example1)\.com
RewriteRule ^(.*)$ http://example.com/no2/$1 [R=301,L]


I notice that the redirect is not happening..Instead the old domain loads as is without any redirect or error.

If I am not wrong, the code reads as follows:
- Checks for www.example1.com or example1.com and pulls everything (.*) and redirects it to example.com/no2

This appears pretty logical to me, though I find it weird that the redirect did not happen at all..

g1smd

12:50 pm on Apr 24, 2010 (gmt 0)

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



It doesn't pull anything.

It checks the requested URL value, and if it matches all the patterns it tells the browser to make a new request for the new URL.

When the browser makes the new request for the new URL, the requested URL now does not match the pattern so processing continues to the content delivery phase.

It is the browser that resolves relative links on the page, so if images, CSS and JS fail to load, you need to adjust the links on those pages to tell the browser to ask for the right URL for those objects. In almost all cases, relative linking will cause problems.

jdMorgan

1:08 pm on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No. Correcting the quote above, it
Checks for NOT www.example1.com or example1.com and matches any URL-path request (.*) and redirects it to example.com/no2/


When preceding a regex pattern, the "!" character is a unary negation operator in mod_rewrite.

So in this case, the redirect will only be invoked if the requested HTTP_HOST is NOT example1.com and NOT www.example1.com. Therefore, "it doesn't work" the way you want it to, because the pattern should be
"!^(www\.)example\.com"

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

The "domains" in both lines should be same.

The second set of parentheses in your RewriteCond is not needed, because the matched domain is not back-referenced in any other RewriteCond or the RewriteRule.

Again, I encourage you to study the documents cited in our Forum Charter [webmasterworld.com].

Jim

anand84

1:10 pm on Apr 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you Jim and G1smd..Your advice means a lot..But there has been some error that has been recurring. So, what I intend to do now is create a new folder and redirect there (instead of redirecting within the same folder that is apparently causing redirects).

I shall update you on the same once that is done. Thanks again.

jdMorgan

3:16 pm on Apr 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might find it helpful to use the "Live HTTP Headers" add-on for Firefox to examine the HTTP transactions between your browser and your server. This may give yo some useful information -- especially if an additional redirect is being invoked by *other* code in mod_rewrite or in a script, leading to a redirection loop.

I'm sorry you're having so much trouble with this -- the redirect code in this thread is really quite trivial, but something is happening that we here cannot "see," and therefore it is nearly impossible to "debug it through a forum." So I'm afraid it's down to you to investigate and find the problem.

Jim