# How to redirect serveralias to the correct domain



## AhrenBa (Jul 21, 2005)

Ok. Let's say you have a virtual host that looks like this (obviously it's missing a few things, but I am just showing the relevant part):

<VirtualHost 10.1.2.3:80> 
ServerName example.com 
ServerAlias www.example.com example2.com www.example2.com> 
DocumentRoot /www/htdocs/site_example
</VirtualHost>

So, obviously this will direct www.example.com, example2.com, and www.example2.com to the folder on the server called site_example.

HOWEVER, the URL in the address bar (in the browser) will not turn into example.com . Instead, it will stay as the original requested one.

Now, how would one go about directing all those aliases to example.com so that it shows example.com in the address bar?

Would you use an .htaccess or mod_rewrite? If so, how would you do this?

Because, if you made a .htaccess in the site_example direction like

redirect 301 / http://example.com

It wouldn't work because it would just loop, right?


----------



## Squashman (Apr 4, 2003)

I believe a mod rewrite would work. You could also do it with DNS to web redirect example2.com to example.com.


----------



## AhrenBa (Jul 21, 2005)

Sohow could you create an .htaccess with mod_rewrite that would do this?


----------



## Squashman (Apr 4, 2003)

I was never good with mod-rewrite and I don't have an Apache or Linux server to test it on anymore.


----------



## metweek (Jun 7, 2003)

AhrenBa said:


> Ok. Let's say you have a virtual host that looks like this (obviously it's missing a few things, but I am just showing the relevant part):
> 
> <VirtualHost 10.1.2.3:80>
> ServerName example.com
> ...


It shouldn't loop if you declare a rewritecond statement

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]


----------



## AhrenBa (Jul 21, 2005)

Thanks, can you explain what that mod_rewrite is saying to do and what I would input into that? Thanks!

EDIT: Or could you just write the statement in terms of the virthost entry I had in my first post? Thanks.


----------



## Squashman (Apr 4, 2003)

I am not sure if Mod Rewrite will change the Domain name in the address bar after doing a little more research. I could be wrong though. I know if you just do a web redirect with your DNS settings it will work.

For the above rewrite to work just put example2.com in the rewrite condition and example.com in the rewrite rule.


----------



## AhrenBa (Jul 21, 2005)

Thanks for the help. 

How do people generally redirect multiple domains to one? You mentioned the DNS, it is that really the most common way?

The only way I can think that would work is to create a folder for each domain, put an .htaccess in each and then make a virtual host entry for each. However, that can't be the way most people do it.


----------



## Squashman (Apr 4, 2003)

AhrenBa said:


> Thanks for the help.
> 
> How do people generally redirect multiple domains to one? You mentioned the DNS, it is that really the most common way?
> 
> The only way I can think that would work is to create a folder for each domain, put an .htaccess in each and then make a virtual host entry for each. However, that can't be the way most people do it.


You could do that as well. Whatever works for you. I actually do redirects with my DNS but that is what works for me as seems the easiest way to do it.


----------



## AhrenBa (Jul 21, 2005)

Ok, cool, thanks.

What is the most common way for people to do it?


----------

