Mathias Bynens

Removing ‘www.’ from your URL

Published · tagged with Apache, HTTP, PHP

In case you hate the ‘www.’ part in your website’s URL (like I started doing about two months ago) you should let others — including search engines — know about that. This is simply a matter of redirecting every page accessed through a www. URI to its non-www. equivalent.

Here are two ways to accomplish this: through PHP, or through the use of .htaccess mod_rewrite rules.

The PHP method

if ('' !== $_SERVER['HTTP_HOST']) {
header('Location:' . $_SERVER['REQUEST_URI'], null, 301);

This script sends headers that redirect the browser to the non-www address and tells search engines like Google that the move is permanent so they can update their indexes.

The downside of using this method is that this code has to be included in every page you want it to work in. To me, this isn’t a problem though, as I’m working with a header.php file, which I include in every page of this site.

The mod_rewrite method

An alternative is to put the following in your .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^mathiasbynens\.be [NC]
RewriteRule ^(.*)${REQUEST_URI} [R=301,L]

Obviously, the mod_rewrite method is far easier to implement than the PHP script. However, some servers provide limited .htaccess functionality — and in that case, you can still use PHP.

About me

Hi there! I’m Mathias. I work on Chrome DevTools and the V8 JavaScript engine at Google. HTML, CSS, JavaScript, Unicode, performance, and security get me excited. Follow me on Twitter, Mastodon, and GitHub.


Eric wrote on :

Worth making a note of the fact that, while a 301 redirect will let Google know that your website has moved/changed address/now looks prettier, it will not tell the Yahoo! search engine spider anything. Yahoo! can not handle 301 redirects and will not follow them.

Albert wrote on :

What happens if you don’t have an A record (or similar) for the domain in question? You have: 86400 IN A

…but I don’t have the rights to update my top level domain zone to include an A record.

Simon wrote on :

Late to this party, but I was pretty early to the very first anti-www campaign (see my own domain name, and the "Why web?" page thereon).

A point to note regarding DNS records:

Although you can have an A record (i.e. an actual IP address) associated with your domain stub (technically, with the ORIGIN in the zone file), this can mean an awful lot of work if for any reason you need to change the IP of your webserver.

Especially if you happen to have a number of domains all DNS hosted in separate places that need to point at the same machine which serves pages for all the sites using on a name-based virtual hosts basis.

Unless you have complete control over the zonefiles for all the domains (and a lot of ISPs don’t allow this level of zonefile control), you’ve often got to liaise with someone else — possibly many other people — in order to change your own server’s IP address (you may be switching ISPs, for instance).

Even if you do have the level of control over all the zonefiles, what a pain to have to update lots of separate ones to temporarily reduce the TTL value for the refresh to a couple of hours instead of several days, wait for lots of separate DNS propagation delays so that change takes effect, then change the origin’s A records to the new IP address, wait again for the shorter, temporary, TTL refresh to expire, finally re-update the TTL to a sensible value again… yeuch!

Contrast that approach with sticking to using machine.domain.ext — but with a sensible machine name such as "web" rather than "www" (1 syllable instead of 9), and you can have the various different zonefiles’ entries for machine.domainA.ext, machine.domainB.ext etc all point at a CNAME record instead of a hardcoded IP address. The Whole Point: you can’t have the domain origin point at a CNAME record, so that’s why I say stick with using a machine name (just not www).

The great benefit to be had here is that the CNAME record can be for something generic, e.g. in the domainA.ext zonefile you might have:

machine IN CNAME webserver1.otherdomain.ext.

…and similarly in however many other zonefiles you have to manage.

It’s then just the single otherdomain.ext’s zonefile that ever needs to be updated in the event of an IP renumbering exercise in future.

The tiny downside is an initial "double lookup" the first time someone tries to resolve machine.domain.ext to an IP address, the first lookup gets the CNAME and then the second resolves that to the actual IP.

Believe me, when you’ve got customers who are off registering domains all over the place and wanting to point them all at one box it makes life a heck of a lot easier to get them to put a CNAME in their zonefile for the webserver being hosted on their behalf. I get to keep direct control over what IP the machine with that CNAME in my domain ultimately resolves to, than to try and chase down and coordinate potentially dozens of different ISPs.

As someone once said, the only problem that can’t be solved by another level of indirection… is too many levels of indirection. I think one additional DNS lookup for the first time a URL is requested by an individual is a sufficiently small price to pay.

Feel free to disagree, of course, but only after you’ve changed the IP on a live webserver that serves the pages for lots of different domains without going mad in the process :)

Popola wrote on :

I would like to know how to make it remove only www. — can anyone help with that?

wrote on :

Popola: Here you go:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mathiasbynens\.be$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

AllanUp wrote on :

I agree with, and configure my site in accordance. However I do have some concerns around branding and SEO that people should be aware of.

  1. Many online text editors will pattern match www.x.y as a web address and automatically create it as a hyperlink. So all those users casually talking about your web site in newsgroups and blogs etc will be creating links to your web site. More links means higher search engine ranking. If you omit the www. no auto linking occurs and you miss out on that valuable link.

  2. The general (non technical) public know that www. means you’re talking about a web address, something that will work in a web browser. Simply writing may not be enough for to recognise that it’s a web address, especially if point 1 applies as well! Instead you need to write now we know it’s a web address. So that’s fine as long as somebody technical is writing, but for joe public (http:// is much harder to write and remember than www.).

Using www. unnecessarily bloats web addresses, I like short and simple, but omitting www. may hurt your search engine rankings in the long run.

Jeff wrote on :

It doesn’t matter whether your canonical address is with or without the www, it only matters that one redirects to the other. The people behind need to stfu. Even Google are still using www.

Also, PHP and Apache? LOL!

Leave a comment

Comment on “Removing ‘www.’ from your URL”

Your input will be parsed as Markdown.