May
4
htaccess Require the www For Domain URL
May 4, 2008 |
While some people prefer to not use “www” in their web site URL, some prefer that it always be there. Either way, it can sometimes be useful to have a single canonical name by which your site can be accessed.
To force the use of “www” when viewers are reading your site, you can use the following mod_rewrite rule in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.net$ [NC]
RewriteRule ^(.*) http://www.example.net/$1 [L,R]
This will ensure that anyone just typing your domain name in their browser ends up with the full name instead.
Reasons for Requiring WWW
Many domains offer a variety of services across many different hosts. These hosts are usually referenced using the server’s fully-qualified domain name (FQDN). This name takes the form of host.domain. So in the FQDN foo.example.net, you can see that the host “foo” is part of the “example.net” domain.
By convention, the web server hosting the main site for a domain can be referenced by using the hostname “www” (which is itself usually an alias, or CNAME record, to the servers actual FQDN). If you want to see the main site for example.net, a good bet would be to try going to www.example.net. Other services are generally offered using the same naming convention. Our example domain’s mail server might be mail.example.net, while their FTP server might be ftp.example.net.
Configuring your web server to also answer to just “example.net” is often done as a convenience for users or as a way to shorten your URL. There is no requirement that this shortcut work and, while most domains do utilize this, many do not.
Notes
While most domains configure their main web server to answer to both forms, most do not bother with this type of redirection from one to the other. Ultimately, as long as the content served is the same, it doesn’t usually matter what name is used to get to a given site.
One consideration when deciding whether to redirect “example.net” to “www.example.net” (or vice-versa) is that of Search Engine Optimization (SEO). Many search engines blacklist sites that “mirror” their site (have the same content at multiple URLs) under different domains and/or sub-domains. Spiders may not blacklist for a “www” mirror, but better to play it safe by picking one and redirecting the other to it.
There was a small movement at one time to get people to use “web” instead of “www” in their site names, thinking that it makes just as much sense and it’s easier to say “web.example.net” instead of “www…”
Similar Posts
- htaccess Remove the www From Domain URL
- Create Custom Error Pages Using htaccess PHP
- Block people from Certain URL Using htaccess
- Google Text Translation Using htaccess
- Block IPs Using htaccess
- Use htaccess for 404 Redirect
- htaccess Explicitly Define Default Index File
- htaccess gzip for Faster Loading and Bandwidth Saving
- htaccess Permanently redirect file or directory
- Use htaccess with Site Maintenance Page
- Apache htaccess Prevent Users from Uploading and Executing Files
- Set Local Timezone Using htaccess
- Use htaccess to Fake Different File Extension
- Deny Access to inc Files Using htaccess
- Use htaccess to Deny Access Directory Listing
- htaccess Limit the Number of Concurrent Visitors to your Website
- Useful mod_rewrite Resources
- Remove File Extension Using htaccess
- htaccess Conditional Loop Redirect
- Force Files Like PDF Download using htaccess
- htaccess Deny Diractory Access During a Specific Time
- Use htaccess to Deny Access to hidden Files
Comments
1 Comment so far



































nice research material, thanks for sharing.