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

I have 3 domain names for one site. e.g example1.com, example2.com, example3.com. I want to redirect example2.com and example3.com to example1.com. I know this has to do with redirecting, but where do I put the redirect rules? in apache, or .htaccess. Any pointers? Currently I have this set up as 3 different virtual hosts pointing to the same folder. Thank you.

share|improve this question
    
You can put the redirect rules in several places, including Apache's configuration and .htaccess files. –  Chris S Mar 23 '11 at 19:45
    
    
thanks for the link Chris –  berto77 Mar 23 '11 at 21:08

2 Answers 2

You can do this with either .htaccess or by add Rewrite rules in the VirtualHost config directly. The apache docs here would tell you not to use .htaccess if you can edit the server config. If you do this in .htaccess when you don't need to, apache has to first check if this file exists for every page hit.

For the redirect itself, you could use something like Redirect 301 / http://example1.com if you want http://example2.com/foo/bar/baz to redirect to http://example1.com/ ; if you want it to actually redirect to http://exmple1.com/foo/bar/baz (retaining the rest of the url after the hostname), you can instead use RedirectMatch 301 ^(.*)$ http://example1.com$1 The docs for Redirect and RedirectMatch are here

share|improve this answer
    
RewriteCond %{HTTP_HOST} ^(example2.com|example3.com) [NC] RewriteRule ^(.*)$ http: //www.example1.com$1 [R=301,L] ...I tried this but it didn't work. I placed that in the virtual host definition of the example1.com –  berto77 Mar 23 '11 at 20:47
    
yes, that achieves the same thing with mod_rewrite instead of mod_alias (in a way that I think is less readable :) ) And you'd better add "RewriteEngine on" to the beninning of that –  stew Mar 23 '11 at 20:49
    
Where do you put the redirect rule? I have access to apache config file. Right now I have it in the <Directory></Directory> tags of example1.com –  berto77 Mar 23 '11 at 20:51
    
In the <VirtualHost> section, not inside any subsection. since you are going to redirect all traffic from this VirtualHost, any <Directory> or <Location> or <Files> like subsections are going to be irrelevant anyway –  stew Mar 23 '11 at 20:53
    
Of the example2 and 3? or example1? –  berto77 Mar 23 '11 at 20:57

You can do this with DNS. In your external DNS records point example2.com and example3.com to the external IP of example1.com.

share|improve this answer
    
This doesn't "redirect", it just shows the example1 site with the other addresses. –  Chris S Mar 23 '11 at 19:44
    
so I need to set up DNS records for example2.com and example3.com? I'm hosting with slicehost –  berto77 Mar 23 '11 at 19:45
    
True, it doesn't "redirect", however, it does depend on the desired result. If the desired result is to have example2.com & example3.com as aliases for example1.com then this works. –  Cheekaleak Mar 23 '11 at 19:51

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.