Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I want to redirect mysite.com or http://mysite.com or www.mysite.com or any other format given my user to http://www.mysite.com, I'm able to achieve this by rewriting following lines in my .htaccess file

Rewritecond %{http_host} ^mysite.com
RewriteRule ^(.*) http://www.mysite.com/$1 [R=301,L]

But I want do this from Apache,So I've added following line in Virtual host conf file of the site and removed above two lines from .htaccess

Redirect 301 / http://mysite.com/

But whenever I'm trying to access the site following error is displyaing,

Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

where I'm doing the wrong ?

share|improve this question

marked as duplicate by Simon Hayter, John Conde Mar 31 '13 at 16:54

This question was marked as an exact duplicate of an existing question.

isn't this a predestined question for serverfault? – uberconversion Mar 25 '13 at 10:07
I thought this question might suitable here, if not please flag it – Mahesh.D Mar 25 '13 at 10:08
i flagged, but to be of help, the 310 error indicates that you have a redirection loop going on. i also suspect that the "Redirect 301" is wrong, seems to me you are redirecting from the root directory to mysite.com (which is also the root directory)... hence you get an infinite loop. – uberconversion Mar 25 '13 at 10:12
@DKOATED: Thanks – Mahesh.D Mar 25 '13 at 10:14
@DKOATED: Thanks, after removing the / then its working as expected. – Mahesh.D Mar 25 '13 at 10:18
up vote 0 down vote accepted

What you want is this:

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

which was copied directly from here. You really ought to read through that page and understand it.

Your current rewrite rule, as it stands, sends every request to a local server path that your apache config probably cannot deliver ('/' is the root directory of your server not the root of your website). Apache in turn will try to respond to that with an error page, which in turn gets redirected to the '/' again, hence an infinite redirect.

The above code only redirects requests NOT bound for www.example.com and preserves the rest of the URL when it redirects.

share|improve this answer

Your directive causes an infinite redirection loop.

The reason is that a 301 gets returned to the browser and that host and path are really separate things, despite forming a URL. When a browser sees a 301 redirect to http://site.com/ it interprets it as find site.com and ask for the / path from the standard HTTP port.

With your unconditional rule, which is valid for site.com, the 301 is returned again which sends them in an infinite loop.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.