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

qwerty
  • 5,166
  • 17
  • 56
  • 77
  • Because the `[OR]` at the end of each domain was not in your answer, and that solved my problem. – qwerty Jul 01 '13 at 09:42

4 Answers4

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]
Nico W.
  • 103
  • 1
  • 7
Muddassar Ahmad
  • 516
  • 4
  • 8
  • 3
    Domains with leading `www` will not be affected by this. You can include such domains by using: `RewriteCond %{HTTP_HOST} ^/?(?:www\.)?domain1.com` – Avatar Nov 09 '16 at 08:47
  • 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 error – PandaWood Mar 25 '18 at 01:59
  • And how can I handle it if somebody is calling one of my domains with https? E.g. https://domain1.com. – Patrick Münster Jul 04 '18 at 16:15
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.

medoingthings
  • 2,985
  • 3
  • 17
  • 17
  • ì guess that if i complete the `RewriteCond` address to `www.newdomain.com`, it's gonna also redirect any subdomain to www. am i right? – robotik Sep 07 '16 at 17:26
  • 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? – melat0nin Jan 18 '18 at 14:15
  • 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 do – clockw0rk Jul 19 '21 at 09:32
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!

Shayan Amani
  • 5,787
  • 1
  • 39
  • 40
-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

tread
  • 10,133
  • 17
  • 95
  • 170
  • 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). – qwerty Jun 28 '13 at 14:12
  • `RewriteCond %{REQUEST_URI} !^newdomain.com` I think this could work – tread Jun 28 '13 at 14:29
  • 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? – qwerty Jun 28 '13 at 14:54