The consensus is clear. You don’t like websites that force you to use the “www” sub-domain, making everyone type www.example.com rather than just example.com. So how do you fix it? With Apache, the world’s most popular web server software, it’s easy.
There’s three things to do.
- Make sure you have DNS entries to provide an IP address for both the domain example.com itself and the www sub-domain.
- Make sure you have Apache configured to serve out your website to both of those domains.
- (Optional) Add a URL re-writing rule so that whichever way users type the website address, their browser will always display your preferred default, with or without the www.
Step 1 can be done in two ways. You can either provide DNS A records for both example.com and www.example.com to your web server’s IP address. Or you can provide an A record for example.com and then a CNAME record for www.example.com pointing it to example.com.
If your internet hosting provider doesn’t provide you with a control panel to do this yourself, request it from their technical support team. If your hosting provider refuses to do this, please let me know so we can name and shame.
As Martin Barry points out, the DNS specifications don’t allow you to CNAME the base domain example.com to www.example.com.
Step 2 involves a specification in Apache’s httpd.conf
file. Generally a hosting provider won’t let you anywhere near this file, and for good reason. So you’ll have to request it. If you manage your own webs server, you’ll need to make sure there’s a section like this for your website.
<VirtualHost 192.0.2.1:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /home/example/public_html # or whatever you website uses
</VirtualHost>
This assumes that your website is one of several virtual hosts run from the same web server. There will probably be other lines within the VirtualHost
directive too. Leave them well alone.
Step 3 involves having the mod_rewrite module installed in Apache, and adding a few lines to the file .htaccess
in your website’s root directory. The first half has to be done by your web server’s systems administrator, although I imagine most hosting providers already have it installed. Then add this to your .htaccess
file.
# Standardize URL to http://example.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule (.*) http://example.com/$1 [R=Permanent,L]
</IfModule>
Obviously you replace the example.com pieces with your own domain, and you need to backslash-escape each dot in the domain.
It’s just as easy to do it the other way, forcing the www to appear no matter what the user types. You might want to do that so the website matches your marketing materials. But I’m not going to show you how to do it because it’s lame.
This post was written very quickly and quite early in the morning. If you think it could be improved, please add your suggestions in the comments. And do let me know how to do this for other web servers, especially that Microsoft one.
s/no matte what/no matter what/
And the pink on white of your <code> sections is a bit hard on these old eyes.
Now pardon me while I go check all my domains to see if they work with and without http://www., and translate one form to the other reliably… Thanks!
(Oh, and did you know that your Javascript preview widget bears only the very slightest resemblance to what actually gets posted? In particular, I can type dubya-dubya-dubya-dot and it shows up unchanged in the preview, but develops a sudden and unnecessary aitch-tee-tee-pee-colon-slash-slash when the comment is posted. Minor but worth noting.)
@Eric TF Bat: Typo fixed, thank you. You’re right about the styling on the <code> bits. That’s the default in the Tarski WordPress theme I’m using. I really should change that one day. And thanks for the heads-up about the other problems.
FWIW, SEO types will tell you 3) is very much non-optional if you’re relying on organic search to drive traffic to your site.
If you rely on search traffic, it’s probably a very good idea to choose one of http://www.domain.com or just domain.com, and redirect all requests to the non-chosen one to the appropriate page on the chosen (sub)domain. (Which can also be done with slightly more advanced mod-rewrite fu)
You should probably also go into your Google Webmaster tools account and set the Preferred domain (in Site configuration -> Settings) to match as well so Google knows which one you’d prefer to use.
Oh, and in support of Mr Fruit Bat, not only does your (or WordPress’s or Tarski’s) preview window blow, but comment post handler also not-so-randomly added http:// into my post apparently because I typed something it thought was a domain name, so it assumed I _obviously_ intended to provide a link to a webserver running on the machine pointed too by that domain… Which blows on it’s own, but doublely blows since the preview doesn’t hint that it’s going to do that…