Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have 3 domain names that point to the same website.

domain1.com, domain2.com, domain3.com

I would like to add in the .htaccess file a way that if someone is trying to reach domain2.com the server will redirect him to www.domain2.com.

same for domain1.com will go to www.domain1.com

I would like to do that in ONE single .htaccess since they point to the same place.

now I have something like this:

Options -indexes 
Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.domain1\.com [NC] 
RewriteRule ^(.*)$ http://www.domain1.com/$1 [R=301,L] 

RewriteCond %{HTTP_HOST} !^www\.domain2\.com [NC] 
RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L] 
share|improve this question

migrated from stackoverflow.com Aug 13 '11 at 10:40

This question came from our site for professional and enthusiast programmers.

2 Answers 2

I'm agree with Rikon but here is your answer:

RewriteCond  %{HTTP_HOST}  ^domain1\.com$ [OR]
RewriteCond  %{HTTP_HOST}  ^domain2\.com$ [OR]
RewriteCond  %{HTTP_HOST}  ^domain3\.com$ 
RewriteRule (.*)  http://www.%{HTTP_HOST}/$1
share|improve this answer

Could I suggest a different approach: a 301 Permanent Redirect

This will be much better for your SEO.

Here's a good website that walks through how to do a 301 for each programming language (including how to do it with .htacess): http://www.seoworkers.com/seo-articles-tutorials/permanent-redirects.html

Here's the quote from the web site on how to do this with .htaccess:

II Redirecting with .htaccess

If you are on an Linux/Apache server, or a Microsoft Windows Server that can run Apache modules, you can do your redirects with an .htaccess file.

The .htaccess file is a very powerful tool, but can wreak havoc on a site if not implemented correctly. Always save a backup of any existing .htaccess file before attempting any changes.


Redirection with mod_rewrite

The following lines redirect the client to a new location. In this case, a request for the file contact.php is sent to the file contact-us.php:

rewriteEngine on rewriteRule ^contact.php$ http://www.dexterityunlimited.com/contact-us.php [R=permanent,L]


Redirect all non-www traffic

The code below will direct all traffic which does not contain the www in the URL to the same page, but now including the www. And that will clean up the canonicalization problem that arises when the www version of your site gets indexed along with the non-www version:

rewriteCond %{HTTP_HOST} ^dexterityunlimited.com$ rewriteRule ^.*$ http://www.dexterityunlimited.com%{REQUEST_URI} [R=permanent,L]


Original source: Search Engine Friendly Permanent Redirects - SEO Workers http://www.seoworkers.com/seo-articles-tutorials/permanent-redirects.html#ixzz1Us3DQxtj Under Creative Commons License: Attribution No Derivatives

FYI: Here's the quote (from the above site) on why to do a 301:

I am certain there are some of you asking this question. The answers are many, depending on your particular situation.

Are there links to your site that are outdated, but you have no way to change them? Simply create a redirect that will take visitors using that outdated link to the most appropriate new page on your site.

Is there a link in a directory somewhere that is misspelled, or is missing the www? Use one of the methods above to correct the situation at the server.

There are many ways for you to control what people see and where they can go on your web site. Take some time to learn a few tricks, and you might find that your traffic increases because you aren’t leaving anyone behind.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.