Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to redirect multiple hostnames to only one, for example, if you enter any of:

you must be redirected to

I have installed a virtual host with as ServerName and the others as ServerAlias, and use mod_rewrite to check if the hostname was right, and if not do a redirection.

The problem is that everything seems to be served with the ServerName directive hostname, so mod_rewrite always gets, which is consistent with the apache2 documentation:

For example, suppose that you are serving the domain www.domain.tld and you wish to add the virtual host www.otherdomain.tld, which points at the same IP address. Then you simply add the following to httpd.conf:

NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain

<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain

You can alternatively specify an explicit IP address in place of the * in both the NameVirtualHost and directives. For example, you might want to do this in order to run some name-based virtual hosts on one IP address, and either IP-based, or another set of name-based virtual hosts on another address.

Many servers want to be accessible by more than one name. This is possible with the ServerAlias directive, placed inside the section. For example in the first block above, the ServerAlias directive indicates that the listed names are other names which people can use to see that same web site:

ServerAlias domain.tld *.domain.tld then requests for all hosts in the domain.tld domain will be served by the www.domain.tld virtual host.

Is there any apache2 directive to avoid this feature and get request server by with the hostname of the ServerAlias they match?

Or must I create another virtualhost just for redirection?


share|improve this question
up vote 0 down vote accepted

To be fairly honest with you, the best approch i can see for this would be if you setup your main domain normally like this:

<VirtualHost *:80>
    DocumentRoot /www/domain 

Then you create a new virtualhost that will hold all domains you want to redirect like this:

<VirtualHost *:80>
    DocumentRoot /www/redirect_folder 

Inside that folder make a simple index.php page that summons the 301 so any domains hold in there will be redirect to your main domain with the 301 code.

Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location:" );

Why do you think it is better this way ?

This way you won't have to keep updating a bunch of places everytime you have a new domain to hold and redirct to your main domain and it won't be serving your users with the current name but will actually redirect them to your main domain in question.

If you are the server owner you can make it even better, you can put the 2nd virtualhost as the first virtualhost in your httpd.conf of vhost.conf file and whenever you hit the IP of your server it will lead you to the redirection page which will lead your users to the main domain in this case instead of having to set a bunch of ServerAlias you can just create the DNS A record for that given subdomain or domain leading to your IP and the server will take care of the rest.

In this last case all you would need for your virtual host would be:

<VirtualHost *:80>
    DocumentRoot /www/redirect_folder 

as you dont need the ServerAlias since every and each request that hits your server IP will go to your first vhost.

In addition if you wanted to do this using .htaccess, it would be something like this i belive:

RewriteEngine on
rewritecond %{http_host} ^ [nc]
rewriterule ^(.*)$$1 [r=301,nc] 
share|improve this answer

It all depends on the configuration order in this case.

If you have a setup like this:

<VirtualHost>ServerAlias *.domain.tld</VirtualHost>
<VirtualHost>ServerName subdomain.domain.tld</VirtualHost>

Than people will always end up at the first vhost. However, if you define them like this, it works:

<VirtualHost>ServerName subdomain.domain.tld</VirtualHost>
<VirtualHost>ServerAlias *.domain.tld</VirtualHost>

Just remember that wildcards should always be at the bottom.

share|improve this answer

Your Answer


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.