1.6. Making Sure Your Web Site Loads With and Without the "www" Prefix

Problem

You need to make sure that your web site can be accessed both with and without the "www" prefix.

Solution

If your web site won't load without the "www" prefix—or it won't load with "www"—then you may need to make a change to your DNS record. Some hosting providers allow customers with higher end accounts to change their own DNS records, but use caution if your account includes this feature and you're not sure what you're doing. If you're in doubt, contact your hosting company for clarification or guidance.

If you have access to the command-line network tools nslookup or dig, either on your own PC or through a Telnet shell provided by your hosting account, you can investigate the details of the various listings in your DNS record without changing them. Some web-based tools (see the "See Also" section in this Recipe) can access the same DNS record information if you do not have access to dig or nslookup.

In the example below, a dig request on www.daddison.com shows it to be a CNAME, or canonical name, listing for daddison.com. A CNAME listing is an alias to the main, or A RECORD, listing in the domain name's DNS record. Requests for either web site address—with or without the "www"—are answered with my web site:

	Lookup has started …

	; <<>> DiG 9.2.2 <<>> www.daddison.com any
	;; global options: printcmd
	;; Got answer:
	;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40212
	;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 1

	;; QUESTION SECTION:
	;www.daddison.com. IN ANY

	;; ANSWER SECTION:
	www.daddison.com. 3600 IN CNAME daddison.com.

	;; AUTHORITY SECTION:
	daddison.com.     3600 IN NS ns22.pair.com.
	daddison.com.     3600 IN NS ns0000.ns0.com.

	;; ADDITIONAL SECTION:
	ns0000.ns0.com.   7110 IN A 216.92.61.2

	;; Query time: 98 msec
	;; SERVER: 151.164.20.201#53(151.164.20.201)
	;; WHEN: Wed Apr 20 15:50:18 2005
	;; MSG SIZE rcvd: 113

Discussion

In the classic 1963 farce "It's a Mad, Mad, Mad, Mad World," an all-star cast from Hollywood's Golden Age raced against each other to find a treasure hidden under a big "W." In the late 1990s, during the Internet's first (we hope) Golden Age, legions of dot-com entrepreneurs also sought riches, in this case, under "www." With the wisdom of hindsight—and perhaps with a renewed appreciation for the farcical and tongue-tying nature of hyper-alliterative repetition—many web sites now present themselves on the World Wide Web sans "www."

But old habits die hard. Although it's now rare to find a reputable web-hosting provider that does not configure its servers to respond to browser requests for a web site both with and without the "www" prefix, the visitors to your site—especially novice web users—may assume that typing in the "www" is a requirement for getting to your web site.

From the perspective of the hosting company, the prefix is simply a shorthand for the type of Internet resource and server at their data center that handles the request. In a typical scenario, the DNS record for your domain has been configured to redirect requests starting with (or without) "www" to a host server that handles web requests. The DNS record may also have entries for "ftp," "mail," or other services enabled on your domain that are handled by other host servers.

There are also some good reasons to direct all of your visitors to your web site without the "www" prefix. Not only is it a time-wasting mouthful when spoken aloud in a broadcast or voicemail auto-attendant message, but some web site functionality—such as session cookies and SSL certificates—may be valid for web site addresses without the "www," but not with it.

If your web server has the rewrite module enabled, you can create rules that tell Apache to seamlessly change the URL requested by the browser to something else. For example, requests for http://www.domain.com become http://domain.com. To do this, create or modify the .htaccess file in your web root directory with a rewrite rule to remove the "www." from browser requests to your web site. First, find or create an .htaccess file. Then copy into it the code shown below, replacing domain.com with your domain name:

	RewriteEngine On
	RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
	RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

See Also

No-WWW is an online campaign to purge the "www" from web site addresses. Visit the site at http://no-www.org. Sam Spade Tools offers a variety of web-based domain tools at http://www.samspade.org/t/.

Get Web Site Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.