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.
Comments (30)
Listed below are the responses for this entry.
Trackbacks & Pingbacks (4)
Listed below are resources on the web that mention this article.
-
- JezzJournal:
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
-
- Math Jazz: Web Design Canon:
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
-
- Oliver Zheng: WWW’s are deprecated:
WWW’s are deprecated
These two great tricks are taken from Mathias.- Trackback made on May 24th, 2005 @ 3:42 am
-
- Simon Speight: Removing the
www: 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 asuksimon.comand not aswww.uksimon.com.- Pingback made on August 11th, 2005 @ 12:13 am
- Simon Speight: Removing the
…
nice to know, I was looking for this for ages
Indeed. Cool website you got there :D
“coolest” and “hippest” on the web
check out my powerpointstuff
Why would you want to though? Both ways are a fine way to access a site.
It works! I’m using the mod_rewrite option now. Nice. :ta:
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!
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=301thing though.Thanks for sharing.
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 comand the other half towhatever 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=301thing sends out the 301 Moved Permanently error message. Your links aren’t working by the way… :???:Actually, I was being sarcastic too (in both comments).
http://geocities.com/El_Bandano
? my completly finished website
(yes idd: sarcasm)
stuff I found on my website today:
Yay!
I know what the 301 means, mathibus. ;)
My site wasn’t working yesterday because my webhost was upgrading some hardware.
… and actually it’s not an error but a status code. :)
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.
Nice hack!
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.
Yahoo! sucks then.
For further information, check out those two sites.
Mathias: As I noted, thanks for pointing me at this. There probably should be a disclaimer with your
.htaccessmethod; “this would break subdomains”. That’s the only harm I see in that versus what Anne uses.Best!
The
.htaccessmethod 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
.htaccessmethod, and his subdomain still seems to work fine, too.Does it break your subdomains? It might be a server thingy if so…
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:
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.
What happens if you don’t have an A record (or similar) for the domain in question? You have:
…but I don’t have the rights to update my top level domain zone to include an A record.
To be honest with you, Albert, I had to Google to find out what an is.
I might be misunderstanding and/or missing your point, but your site works perfectly without the 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.
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 withhttp://and the trailing/as the browser then should assume you know where you want to go and there should not be anything responding oncdr.seat all. The way it works for me now (withwww.) is how I want it, but I am sure others does not and then your tip is good.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 tohttp://www.cdr.se/— your non-www.URI seems to work fine, except when it’s linked.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.
Late to this party, but I was pretty early to the very first anti-www campaign (see my own domain name, and the 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 rather than (1 syllable instead of 9), and you can have the various different zonefiles’ entries formachine.domainA.ext,machine.domainB.extetc 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.extzonefile 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 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 :)
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]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 mypublic_htmlroot, and that seems to be causing some kind of problem, though I’m not sure why. I want to use the.htaccessmethod to do this, so I’ve placed the appropriate lines in the top-most.htaccessfile on my site (the one athttp://maymay.net/.htaccess). It works fine for top-level files (it will redirectwww.maymay.net/fileAtomaymay.net/fileA) but doesn’t work for any other directory (www.maymay.net/blog/index.phpdoes not get redirected tomaymay.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/.htaccessfile, finding a matchingRewriteRule, and upon seeing the Last flag, not advancing to my/.htaccessfile 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.