问题
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?).
回答1:
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 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]
回答2:
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.
回答3:
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
^/?$
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:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\/" [R=301,L]
Good Redirection!
回答4:
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
来源:https://stackoverflow.com/questions/17365397/redirect-multiple-domains-to-one-domain-with-or-without-www-before