OUR BUSINESS is building mobile solutions for YOUR BUSINESS

The internet is going mobile. Give your business the edge - Whether you need to mobile enable your customers, suppliers or staff, we can design and build YOUR SOLUTION... Websites, iPhone and Android.

Configuring Apache2

Articles about configuring Apache 2

Configuring Apache2 Virtual Host to force non-www URLS

I don't know about you, but I have a bit of a bias against using .htaccess files to force rewrite rules. Using .htaccess forces the web server to check for an .htaccess file for every file served, and not only do they have to check the directory your file is in, they also have to check the entire parent directory tree for the .htaccess files. That adds a lot of overhead to every file request.

After days of looking for a better way of forcing the URL's on my website to be http://bgies.com/somefile... instead of http://www.bgies.com/somefile I finally found a really good way of doing it. Of course, this method requires you to have root access to your server, but I just switched from using a shared host to using a cloud-based virtual server, so I now have complete access to everything.

Why do you want to do this? Well.. if you have been following discussions about search engine optimization you will know it's something you should do. Your pages will look a lot more popular to Google if all the hits are for one page, not one with www and one without. In my case I'm using Google Maps in a lot of my applications, and Google Maps has a problem if your key is for a website with www and you send a request from a non www page, so it's much simpler to just force the non-www for all pages. Trust me, there are a lot of good reasons to force one or the other.

If you want the www in the URL for all your pages (God knows why you would want them), but if you do just reverse my instructions :).



This works in Ubuntu 10.0.4 Intrepid... It's what I'm using.. not sure about other Linux distros... or other versions of Ubuntu.

These instructions assume you are using virtual hosts... so substitute your real domain name for yourdomain.com

You just need to edit your virtual host configuration for the site:

From the command line: sudo /etc/apache2/sites-available/yourdomain.com

You will see something like this :


Sorry, the email address had to be disguised to hide it from our own email hider :). We have a plugin that automatically check our articles for email addresses and hides them to cut down on spam. It needs to be a real email address, so change the "admin at yourdomainname DOT com" to a real email address.

Just add the following :

And you will end up with something like this :


That's it... now all of your requests with a www in the URL will be redirected to the same page without a www in the URL... As I said you can change the added part to force the "www"... but please don't... I hate typing it.. and I really hate having to say "www" all the time :).