Mathias Bynens

Removing ‘www.’ from your URL

· 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

<?php
if ('mathiasbynens.be' !== $_SERVER['HTTP_HOST']) {
header('Location: http://mathiasbynens.be' . $_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 ^(.*)$ http://mathiasbynens.be%{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.

Comments

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:

mathiasbynens.be 86400 IN A 82.192.67.21

…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 ^(.*)$ http://mathiasbynens.be/$1 [R=301,L]

AllanUp wrote on :

I agree with no-www.org, 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 abc.com 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 http://abc.com/ 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 no-www.org need to stfu. Even Google are still using www.

Also, PHP and Apache? LOL!

Leave a comment

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

Some Markdown is allowed; HTML isn’t. Keyboard shortcuts are available.

It’s possible to add emphasis to text:

_Emphasize_ some terms. Perhaps you’d rather use **strong emphasis** instead?

Select some text and press + I on Mac or Ctrl + I on Windows to make it italic. For bold text, use + B or Ctrl + B.

To create links:

Here’s an inline link to [Google](http://www.google.com/).

If the link itself is not descriptive enough to tell users where they’re going, you might want to create a link with a title attribute, which will show up on hover:

Here’s a [poorly-named link](http://www.google.com/ "Google").

Use backticks (`) to create an inline <code> span:

In HTML, the `p` element represents a paragraph.

Select some inline text and press + K on Mac or Ctrl + K on Windows to make it a <code> span.

Indent four spaces to create an escaped <pre><code> block:

    printf("goodbye world!"); /* his suicide note
was in C */

Alternatively, you could use triple backtick syntax:

```
printf("goodbye world!"); /* his suicide note
was in C */
```

Select a block of text (more than one line) and press + K on Mac or Ctrl + K on Windows to make it a preformatted <code> block.

Quoting text can be done as follows:

> Lorem iPad dolor sit amet, consectetur Apple adipisicing elit,
> sed do eiusmod incididunt ut labore et dolore magna aliqua Shenzhen.
> Ut enim ad minim veniam, quis nostrud no multi-tasking ullamco laboris
> nisi ut aliquip iPad ex ea commodo consequat.

Select a block of text and press + E on Mac or Ctrl + E on Windows to make it a <blockquote>.