Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

.htaccess - Redirect multiple domains to one domain (with or without www before)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...