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
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:
Then you create a new virtualhost that will hold all domains you want to redirect like this:
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.
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:
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:
It all depends on the configuration order in this case.
If you have a setup like this:
Than people will always end up at the first vhost. However, if you define them like this, it works:
Just remember that wildcards should always be at the bottom.