Create an Apache virtual host which will only redirect
- March 7th, 2010
- By Mr. Nerd
- Write comment
Most internet sites are available through multiple domains. The classic case would be example.com and www.example.com. If you want people to only use one of the domains available, you can either let your website check and redirect (not so elegant) or let Apache do the magic.
To accomplish this, you only need to create a new virtual host for Apache. Here’s a simple example of how to move all visits from example.com to www.example.com:
<VirtualHost *:80> ServerName example.com #ServerAlias example.net www.example.net redirect permanent / http://www.example.com </VirtualHost>
That’s it, from now on, all people who try to reach example.com will be redirected to www.example.com. You can include more hostnames to redirect from by using the ServerAlias directive, in the example above, we’re also redirecting from example.net and www.example.net.