I'm trying to redirect multiple hostnames to only one, for example, if you enter any of:
foo.example.com
www.example.com
bar.example.com
eample.com
you must be redirected to www.example.com
I have installed a virtual host with www.example.com
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 www.example.com
, 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/otherdomainYou 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?
Thanks