The use of 301 rewrite

Chris Sedlmayr

16 Mar 2010

If you are hosting a domain (or multiple domains) using VirtualHost entries, it is bad practice to use individual entries for the same domain just to cover the inclusion or exclusion of the www in the address, amongst other things it is bad for SEO, but of course you still want visitors to be able to go to your site via the 2 starting addresses and

For example you should not have an entry for and another for There are a few ways you can do this but have found this particular one to be effective. Whatever url you go to; or a redirect happens on the that sends you to

But what about people that go to specific pages such as

We use a RewriteRule so that whatever is after the domain itself (in this case services)(When using is shown as It is very simple to achieve this.

Within you VirtualHost entry you need to specify a ServerAlias. So I would have


And then within the vhost file for your project you would have

RewriteCond %{HTTP_HOST} ^sedlmayr\
RewriteRule ^(.*)$$1 [R=301,L]

Which needs to go at the top. That’s it! Hope this is helpful to you at some point.