Enforcing www

Album Cover: Icky Thump

"You can't be a pimp and a prostitute, too."
White Stripes / Icky Thump

Posted on July 05, 2005 1:24 AM in Web Development
Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

I hadn't really noticed until tonight, but up until now it was possible for anyone to view my blog by either pointing their browser to bernzilla.com, or www.bernzilla.com or even something bizarre like voteforpedro.bernzilla.com. Now, this is all fine and dandy, but the one problem this can cause is that repeat visitors who have chosen a site theme other than the default will lose their setting if they visit a different "server." So for instance, if I set the Gecko theme (see the sidebar) as my skin while visiting www.bernzilla.com, I'll still see the default Snow theme if I visit bernzilla.com.

So how is this problem solved? Very easily, thanks to a quick and easy mod_rewrite refresher lesson from Practical Applications. I say "refresher" here because we've been down this road before.

As you can see from that site, forcing the www in all visits to your domain is as simple as adding three new lines to your .htaccess file, and maybe an .htaccess file if you didn't already have one. The change I made to mine looks like the following:

# only allow requests with www in the domain
RewriteCond %{HTTP_HOST} !^www.bernzilla.com$ [NC]
RewriteRule ^(.*)$ http://www.bernzilla.com/$1 [R,L]

The first line (under the comment) looks for any requests in which www.bernzilla.com is not the HTTP_HOST. In matching cases, the second line applies the special rule. We fetch everything following the forward slash after the server name and tack it on to the end of the server name we want to enforce. So in the case where someone has requested something like bernzilla.com/projects.php, they will be redirected to www.bernzilla.com/projects.php.

Pretty nifty, huh?


Ryan on July 08, 2005 at 11:34 AM:

But I wanted to visit bernzilla.com, not www.bernzilla.com...

Can't we come up with a better way of preserving choice, like using all-psubdomain cookies. Accordin to the php web site entry about the setcookie() function:

"To make the cookie available on all subdomains of example.com then you'd set it to '.example.com'. The . is not required but makes it compatible with more browsers. Setting it to www.example.com will make the cookie only available in the www subdomain."

So set the skin preference this way, avoid the rewrite rule overhead, and I can visit voteforpedro.bernzilla.com to my heart's content.

Just my $0.02


Bernie Zimmermann on July 09, 2005 at 12:31 AM:

You bring up a very good point, Ryan. There may be users who prefer bernzilla.com to www.bernzilla.com, but then again they may prefer the Gecko theme to my default Snow theme. They might prefer Arial to Verdana, too, but it's my website so I can do what I want. And then you can go write a Greasemonkey script and do what you want. Or you can write one of those funky browser hacks I'm always reading about that lets you hijack the address bar and put whatever you want there. That's much more fun, isn't it?

Seriously though, your subdomain example is a great solution for the cookie-related problem. I just happen to have a penchant for the www (call me old school) so that supercedes the cookie problem ;)


Suzanne on July 11, 2005 at 2:07 PM:

Bernie -

Hello. I noticed you have many posts of people having problems with gmail. I have had a "Loading" message for over a week now and can't access my account. Any suggestions? I am awaiting some important messages and don't know what to do!

Thank you!


dating look on September 26, 2016 at 11:08 PM:

You'll discover websites given by people or families of people by worldwide and many singles could possibly access these people. Actually, even things that are witout a doubt tangled in place do gain access to them the vast majority of times.


life dating on September 26, 2016 at 11:08 PM:

You must be more unique. Pick a distinct segment current market and stay with it. For case, what in terms of a dating site for the country you reside in or the location you are typically from. Why not consider a faith based dating website or maybe a good ethnic matrimonial website.


udating on September 26, 2016 at 11:09 PM:

We have a simple technique to asking a girl out using a date that won't place a male in that style of intimidating situation while using the fear connected with rejection by her. In truth, this secret boosts the probability of getting a girl out using a date by means of 100%. Secrets is “DON’T consult her for just a date”.


dating pro on September 26, 2016 at 11:09 PM:

It could possibly definitely be good if perhaps preserving with regards to pension were being because quick because environment to get started dating?, directed your individual expense accounts in the correct journey, as very well as going for walks aside for most years, time intended for discover all prepared when you required that.


tech tools foractivism on September 26, 2016 at 11:09 PM:

On this technology centric world, nearly just about every facility together with service under the sun can be obtained at the exact click on the mouse. But one need to be sure on the need independently. Babysitter together with nanny may look like synonymous disorders but actions look may perhaps reveal of which there’s a componant in his or her meanings as well as services they give.


law tech talk on September 26, 2016 at 11:09 PM:

Nowadays chances are you'll hardly endure any articles or reviews, books, Disc disks, shows, movies, documentaries about self-improvement or perhaps self-help devoid of finding a minumum of one mentioning regarding some ‘Universal Law’.


Amber Hina on August 22, 2017 at 10:55 PM:

You'll discover websites given by people or families of people by worldwide and many singles could possibly access these people. Actually, even things that are without a doubt tangled in place do gain access to them the vast majority of times.


Google on September 14, 2017 at 4:17 AM:

it's awesome.


Post Comments

If you feel like commenting on the above item, use the form below. Your email address will be used for personal contact reasons only, and will not be shown on this website.


Email Address:



Check this box if you hate spam.