Math Jazz — Mathias Bynens’s shizzle, y’all



Note: This site might seem inactive… That’s because it is. Don’t worry though, I’m still coding webpages and stuff! If you’re interested, I suggest you get a translator and head over to Qiwi; or you could just check the latest site we’ve been working on: Apotheek Goethals – Debrabandere. Enjoy!

Removing www. from your IRI

In case you hate the www. part in your website’s IRI (like I started doing about two months ago) you can let others—including search engines—know about that by redirecting every page accessed through a www.-like IRI to a non-www. IRI. (What a mouthful.) Matt seemed to have a script like this, but never replied to my post asking for it on the WP forums… Oh, well.

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('HTTP/1.0 301 Moved Permanently');
 header('Location: http://mathiasbynens.be' . $_SERVER['REQUEST_URI']);
}
?>

This script sends headers that redirect the browser to the www.-less address and tell 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 do include in every page of this site.

The mod_rewrite method

Or put this in your .htaccess (thank you Anne):

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

Anne’s original code only redirects from the www. URI, so I slightly modified it in order to make it redirect from even the funkiest domain prefixes.

Why use the PHP script if the mod_rewrite method is so much easier? Well, there are several non-Apache servers on this planet—not that much, but there are. If your server doesn’t allow customizing .htaccess files, PHP is the solution.

Filed under PHP, HTTP · June 8th, 2004

Comments (30)

Listed below are the responses for this entry.

  1. El Bandano:
    This commenter’s Gravatar


    nice to know, I was looking for this for ages

    Comment posted on June 8th, 2004 @ 6:53 pm
  2. Mathias:
    This commenter’s Gravatar

    Indeed. Cool website you got there :D

    Comment posted on June 8th, 2004 @ 8:07 pm
  3. El Bandano:
    This commenter’s Gravatar

    “coolest” and “hippest” on the web
    check out my powerpointstuff

    Comment posted on June 8th, 2004 @ 9:37 pm
  4. X-Istence:
    This commenter’s Gravatar

    Why would you want to though? Both ways are a fine way to access a site.

    Comment posted on June 8th, 2004 @ 10:29 pm
  5. markku:
    This commenter’s Gravatar

    It works! I’m using the mod_rewrite option now. Nice. :ta:

    Comment posted on June 9th, 2004 @ 3:14 pm
  6. MtDewVirus:
    This commenter’s Gravatar

    Sweet!!! I never use the www, so why should I let anyone else? ;) I have to admit, I copied the rule to my htaccess and forgot to replace the domain name… :xx It was only a few seconds though. Haha!

    Comment posted on June 10th, 2004 @ 6:20 am
  7. amon-re:
    This commenter’s Gravatar

    Cool! I did the mod_rewrite trick a while ago on my own site. I wanted to redirect everyone from my old domain to my new one. I didn’t know about the R=301 thing though.

    Thanks for sharing.

    Comment posted on June 10th, 2004 @ 11:17 am
  8. Mathias:
    This commenter’s Gravatar

    El Bandano: Actually, I was being sarcasting. It’s been like ages since I last visited your website, as you never leave your link in the URI field… :-s Never mind.

    X-Istence: For links. It’s not cool if half of the people linking to you are linking to www dot whatever dot com and the other half to whatever dot com. As MtDew Virus said: I never use the www, so why should I let anyone else?

    MtDewVirus: Haha! You know that if your site was spidered by a search engine at that very moment, it would’ve told it that your site is permanently moved to mathibus.com? 8-) Yeah :noddie:

    amon-re: That R=301 thing sends out the 301 Moved Permanently error message. Your links aren’t working by the way… :???:

    Comment posted on June 11th, 2004 @ 4:30 pm
  9. El Bandano:
    This commenter’s Gravatar

    Actually, I was being sarcastic too (in both comments).

    http://geocities.com/El_Bandano
    ? my completly finished website

    (yes idd: sarcasm)

    Comment posted on June 12th, 2004 @ 12:36 pm
  10. El Bandano:
    This commenter’s Gravatar

    stuff I found on my website today:

    Yay!

    Comment posted on June 12th, 2004 @ 12:42 pm
  11. amon-re:
    This commenter’s Gravatar

    I know what the 301 means, mathibus. ;)

    My site wasn’t working yesterday because my webhost was upgrading some hardware.

    Comment posted on June 12th, 2004 @ 2:04 pm
  12. amon-re:
    This commenter’s Gravatar

    … and actually it’s not an error but a status code. :)

    Comment posted on June 12th, 2004 @ 2:13 pm
  13. Mathias:
    This commenter’s Gravatar

    El Bandano: Haha, that Niveau site is like three years old :D I almost forgot I’m such a cool dude. As a side note: I fell in love with your site over again. I love your powerpointstuff :lol:… You even included a Mini-You!

    amon-re: Oh well… error message, status code, … whatever! ;) No really, thanks for pointing that out. I dig your site by the way.

    Comment posted on June 12th, 2004 @ 5:14 pm
  14. Mo:
    This commenter’s Gravatar

    Nice hack!

    Comment posted on June 16th, 2004 @ 7:35 pm
  15. 301:
    This commenter’s Gravatar

    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.

    Comment posted on June 21st, 2004 @ 1:11 am
  16. Mathias:
    This commenter’s Gravatar

    Yahoo! sucks then.

    Comment posted on June 21st, 2004 @ 2:41 pm
  17. Rick Yribe:
    This commenter’s Gravatar

    For further information, check out those two sites.

    Comment posted on June 22nd, 2004 @ 3:09 am
  18. Geof:
    This commenter’s Gravatar

    Mathias: As I noted, thanks for pointing me at this. There probably should be a disclaimer with your .htaccess method; “this would break subdomains”. That’s the only harm I see in that versus what Anne uses.

    Best!

    Comment posted on June 22nd, 2004 @ 3:51 pm
  19. Mathias:
    This commenter’s Gravatar

    The .htaccess method does not break subdomains. Try it out: appartement.mathibus.com still works, because it’s set as a subdomain. This method will only redirect from non-existent subdomains, e.g. pizza.mathibus.com.

    Oh and Markku is using the .htaccess method, and his subdomain still seems to work fine, too.

    Does it break your subdomains? It might be a server thingy if so…

    Comment posted on June 22nd, 2004 @ 8:23 pm
  20. Stuart:
    This commenter’s Gravatar

    There is a third way — don’t register with a www in the first place. I don’t like them either. And where the hell is Kill Bill II?? :crymeariver:

    Comment posted on June 23rd, 2004 @ 12:25 am
  21. dab:
    This commenter’s Gravatar

    I’ve been hating w’s since… I can’t even remember. Long time anyhow. Now I insist that every site on my machine redirects to their non-www domain name. w’s SUCK! ;)

    Whats funny is, that even though I’ve been non-www’ing for at least two years, people linking to my site use www’s. I wonder if they’re blind.

    Comment posted on July 5th, 2004 @ 12:35 am
  22. Albert:
    This commenter’s Gravatar

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

    mathibus.com 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.

    Comment posted on December 3rd, 2004 @ 11:59 pm
  23. Mathias:
    This commenter’s Gravatar

    To be honest with you, Albert, I had to Google to find out what an A record is.

    An A record is part of the zone file. It is used to point Internet traffic to an IP address. For example, you can use an “A record” to designate abc.yourdomain.com to send traffic to your web site at IP address 209.15.32.135. You can also designate xyz.yourdomain.com to go to a separate IP address.

    I might be misunderstanding and/or missing your point, but your site works perfectly without the www period part when I type the non-www. URI in the address bar. However, I can’t link to it. Weird.

    As far as I know, there is no need to create a new subdomain for these redirections to work.

    Comment posted on December 4th, 2004 @ 10:17 am
  24. Albert:
    This commenter’s Gravatar

    It works in the address bar in some browsers, because the browser adds www. and sometimes .com (as that is the most common tld). It should not work if you type it in with http:// and the trailing / as the browser then should assume you know where you want to go and there should not be anything responding on cdr.se at all. The way it works for me now (with www.) is how I want it, but I am sure others does not and then your tip is good.

    Comment posted on December 4th, 2004 @ 5:57 pm
  25. Mathias:
    This commenter’s Gravatar

    No really, I can type in http://cdr.se/ (with the trailing slash!), and it works just fine. (Need I say I’m using Mozilla Firefox?). The address doesn’t change to http://www.cdr.se/ — your non-www. URI seems to work fine, except when it’s linked.

    Comment posted on December 5th, 2004 @ 4:52 pm
  26. Albert:
    This commenter’s Gravatar

    Others might disagree, but I find that being a bug with Firefox. It first looks up “cdr.se” which fails, then it looks up “www.cdr.se” which works. There is nothing that replies at cdr.se, it should show the user that they have gone somewhere the user might not have expected.

    Comment posted on December 5th, 2004 @ 7:04 pm
  27. Simon:
    This commenter’s Gravatar

    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 :)

    Comment posted on January 28th, 2005 @ 7:40 pm
  28. popola:
    This commenter’s Gravatar

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

    Comment posted on July 2nd, 2005 @ 9:25 pm
  29. Mathias:
    This commenter’s Gravatar

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

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.mathibus.com$ [NC]
    RewriteRule ^(.*)$ http://mathiasbynens.be/$1 [R=301,L]
    Comment posted on July 3rd, 2005 @ 9:25 am
  30. Meitar:
    This commenter’s Gravatar

    I’m trying to follow suit with all you guys and remove the www. part in my address as well, however, unlike you guys, my WordPress installation is in a directory other than my public_html root, and that seems to be causing some kind of problem, though I’m not sure why. I want to use the .htaccess method to do this, so I’ve placed the appropriate lines in the top-most .htaccess file on my site (the one at http://maymay.net/.htaccess). It works fine for top-level files (it will redirect www.maymay.net/fileA to maymay.net/fileA) but doesn’t work for any other directory (www.maymay.net/blog/index.php does not get redirected to maymay.net/blog/index.php, and /blog/ is where my WordPress 1.5 install is located). Why is that?

    Is it because WordPress’s permalink structures includes the L (last) flag on its rewrite rules? Is Apache reading my /blog/.htaccess file, finding a matching RewriteRule, and upon seeing the Last flag, not advancing to my /.htaccess file and applying the no-www redirect? And if so, how can I fix this without changing my directory structure nor using the PHP method?

    Thanks in advance.

    Comment posted on December 23rd, 2005 @ 10:13 am

Trackbacks & Pingbacks (4)

Listed below are resources on the web that mention this article.

  1. JezzJournal:
    This commenter’s Gravatar

    The Tweak’age Plan
    Yes, I know it’s already tommorrow (technically), but since I’m up I want to make sure I make clear what I want to get done in the next few days to this site. It’s sort of a clean up, touch up, and adding on all at the same time. There are a few thi…

    Trackback made on August 2nd, 2004 @ 3:04 am
  2. Math Jazz: Web Design Canon:
    This commenter’s Gravatar

    Web Design Canon

    A canon is a rule or especially body of rules or principles generally established as valid and fundamental in a field or art or philosophy.

    Trackback made on December 22nd, 2004 @ 11:31 pm
  3. Oliver Zheng: WWW’s are deprecated:
    This commenter’s Gravatar

    WWW’s are deprecated
    These two great tricks are taken from Mathias.

    Trackback made on May 24th, 2005 @ 3:42 am
  4. Simon Speight: Removing the www:
    This commenter’s Gravatar

    Removing the www
    I had an urge to mess with my site URL once more. Don’t panic, I’m not changing it again, I just want to make sure that it always appears as uksimon.com and not as www.uksimon.com.

    Pingback made on August 11th, 2005 @ 12:13 am