Reputation: 5246
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?).
Upvotes: 19
Views: 42951
Reputation: 516
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1.com [OR]
RewriteCond %{HTTP_HOST} ^domain2.com [OR]
RewriteCond %{HTTP_HOST} ^domain3.com [OR]
RewriteCond %{HTTP_HOST} ^domain4.com [OR]
RewriteCond %{HTTP_HOST} ^domain5.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=permanent,L]
This will redirect all your 18 domains to your new single domain www.newdomain.com
.
Otherwise you can use following code to redirect each domain if they are on separate hosting:
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=permanent,L]
Upvotes: 38
Reputation: 6160
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:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain3\.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain3\.com$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/" [R=301,L]
^/?$
in RewriteRule
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:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\/" [R=301,L]
Good Redirection!
Upvotes: 2
Reputation: 3015
Instead of redirecting a.com
, b.com
, c.com
to newdomain.com
you can do this:
Redirect everything that is not newdomain.com
to http://www.newdomain.com
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]
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.
Upvotes: 19
Reputation: 11098
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=permanent,L]
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 the RewriteCond
.
That would cause a redirect loop
Upvotes: -1