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.
-
You can put the redirect rules in several places, including Apache's configuration and .htaccess files. – Chris S Mar 23 '11 at 19:45
-
possible duplicate of [Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?](http://serverfault.com/questions/214512/everything-you-ever-wanted-to-know-about-mod-rewrite-rules-but-were-afraid-to-ask) – Chris S Mar 23 '11 at 19:45
2 Answers
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
- 9,263
- 1
- 28
- 43
-
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
tags of example1.com – berto77 Mar 23 '11 at 20:51 -
In the
section, not inside any subsection. since you are going to redirect all traffic from this VirtualHost, any – stew Mar 23 '11 at 20:53or or like subsections are going to be irrelevant anyway -
-
to clarify, I have 3
directive. One for each of example1, example2 and example3. So I would put the 301 into the virtual host that I want to direct away from? – berto77 Mar 23 '11 at 21:03 -
you would put this in the virtual host definition(s) for example2 and example3 (the ones you redirect FROM) – stew Mar 23 '11 at 22:00
You can do this with DNS. In your external DNS records point example2.com and example3.com to the external IP of example1.com.
- 11,776
- 1
- 24
- 39
-
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. – HostBits Mar 23 '11 at 19:51