I have about 18 domains that need to be redirected to a new one. It has to work both with or without www prepended.
I've tried this:
<IfModule mod_rewrite.c>
RewriteEngine on
Rewritecond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
</IfModule>
That gives me a redirect loop (and only works with www before, i think?).
My experience after few days rummaging SO and other hosts instructions was disappointing. However, I cherry-picked the best workful parts of all of them and yields the following:
^/?$
inRewriteRule
If you want to redirect www version of the main domain to the non-www version of it, the last two lines should be like this:
Good Redirection!
This will redirect all your 18 domains to your to your new single domain www.newdomain.com Otherwise you can use following code to redirect each domain if they are on separate hosting
Instead of redirecting
a.com
,b.com
,c.com
tonewdomain.com
you can do this:Redirect everything that is not
newdomain.com
tohttp://www.newdomain.com
Credit for this goes to: http://www.raramuridesign.com/blog/83-dev-htaccess-redirect-a-domain-or-multiple-domains.html where it is explained in greater detail.
I tried it out for a client project and it works like a charm.
The
^domain.com
solves the problem of the WWW, so all sub domains will now redirect.Make sure that
http://www.newdomain.com
is not included in theRewriteCond
.That would cause a redirect loop
More info