Redirect multiple domains to one domain (with or without www before)
Asked Answered
V

4

19

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?).

Vizza answered 28/6, 2013 at 13:5 Comment(1)
Because the [OR] at the end of each domain was not in your answer, and that solved my problem.Vizza
C
38
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]
Cramfull answered 29/6, 2013 at 12:12 Comment(3)
Domains with leading www will not be affected by this. You can include such domains by using: RewriteCond %{HTTP_HOST} ^/?(?:www\.)?domain1.comCalmas
Can you explain why this doesn't work when the final domain is "https" and one of the domains is the same name eg bob.com and the final rule is https://www.bob.com - it returns a mis-configuration errorConsuelaconsuelo
And how can I handle it if somebody is calling one of my domains with https? E.g. domain1.com.Clingstone
C
19

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.

Consonant answered 24/2, 2014 at 15:35 Comment(3)
ì guess that if i complete the RewriteCond address to www.newdomain.com, it's gonna also redirect any subdomain to www. am i right?Eldreda
This works great, except when there's something appended to the url (e.g. example.com/members). Is it possible to expand it to automatically include latter parts of the entered URL?Alis
this does not solve the problem. if i have test.com/one and so on up to test.com/ten, and i want only the latter 5 to be redirected to newtest.com/ , this won't doCiaphus
V
2

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]
  • The above format is also according to cPanel style of redirection done in GUI.
  • Redirection of www. version and non-www. version of domains is one of the issues which other solutions (at least I tried em!) couldn't solve it.
  • Pay attention to ^/?$ 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!

Verret answered 8/10, 2016 at 8:14 Comment(0)
A
-1
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

More info

Aphelion answered 28/6, 2013 at 13:14 Comment(3)
Is it possible to match "anything that is not newdomain.com" instead of manually adding each domain as a RewriteCond? This would be a huge hassle for me to do because there are about 18 domains that point to the new one, and the customer is considering adding more soon (and i don't want to have to dig in the .htaccess every time).Vizza
RewriteCond %{REQUEST_URI} !^newdomain.com I think this could workAphelion
That doesn't seem to work unfortunately. I guess i'll have to do it manually for each domain. I assume i can just add multiple RewriteCond lines and it'll work?Vizza

© 2022 - 2024 — McMap. All rights reserved.