How do I redirect all visitors to the “www” version of my Web site (or vice versa)?

This page is showing a generic answer.
To see a more detailed answer customized for you, type your domain name here:

Our Web servers are configured so that visitors to your Web site will see the same content whether they type example.com or www.example.com in their browser's address bar. You can tell other people whichever one you prefer; they'll both work (although we recommend using www.example.com if you have the choice, because it avoids certain potential problems in the future).

Some Web site owners want to make sure that only one address is used. For example, if a visitor goes to example.com, you may want them to be automatically redirected to www.example.com, or vice versa.

You can do this with our service. However, keep in mind that setting up redirection can be tricky, especially if you use subdomains or domain name aliases that point another domain name to your Web site. In fact, it can lead to Web page errors if it is not done correctly.

Therefore, the following changes are not recommended for most users. You should only make these changes if you know how to edit .htaccess files and have a good understanding of the Apache mod_rewrite module. If you aren't comfortable making these changes yourself, you can contact us and ask us to set it up for you. (Please let us know which of these options you would like.)

If you wish to enable this type of redirection, choose one of the following four options. Enter the specified text in an .htaccess file at the top level of your web site.

Keep in mind that these rules will redirect all requests for any file type on your Web site (including images), not just requests for Web pages.

Option 1: Redirecting example.com/dir/anypage.html to www.example.com/dir/anypage.html (same page)

Use this format if you want a request for any URL at example.com to redirect to the same page at www.example.com. (If you want to redirect to the home page of www.example.com, see option 2 below.)

RewriteEngine On
RewriteCond %{HTTP_HOST}  ^example.com [nocase]
RewriteRule ^(.*)         http://www.example.com/$1 [last,redirect=301]

Option 2: Redirecting example.com/dir/anypage.html to the www.example.com home page

Use this format if you want a request for any URL at example.com to redirect to the home page of www.example.com. (If you want to redirect to the same page at www.example.com, see option 1 above.)

RewriteEngine On
RewriteCond %{HTTP_HOST}  ^example.com [nocase]
RewriteRule ^(.*)         http://www.example.com/ [last,redirect=301]

Option 3: Redirecting www.example.com/dir/anypage.html to example.com/dir/anypage.html (same page)

Use this format if you want a request for any URL at www.example.com to redirect to the same page at example.com (which we usually don’t recommend).

RewriteEngine On
RewriteCond %{HTTP_HOST}  ^www.example.com [nocase]
RewriteRule ^(.*)         http://example.com/$1 [last,redirect=301]

Option 4: Redirecting www.example.com/dir/anypage.html to the example.com home page

Use this format if you want a request for any URL at www.example.com to redirect to the home page of example.com (which we usually don’t recommend).

RewriteEngine On
RewriteCond %{HTTP_HOST}  ^www.example.com [nocase]
RewriteRule ^(.*)         http://example.com/ [last,redirect=301]

Subdomains

If you use subdomains such as blog.example.com, you probably don't want your redirections to interfere with that.

The sample "RewriteCond" lines shown above are correctly written to start with a caret symbol (^), making sure they don't match subdomain hostnames. Be sure to use this format in your own rules to avoid such problems.

Aliases

We offer domain name aliases — an extra domain name that points to a separate Web hosting account.

Redirection would be more complicated if you were using an alias since there would be two different domain names pointing to your Web site. The four sample options above wouldn't affect Web requests made via the aliased domain name since the rules do not actually mention the aliased domain name.

So you'd need to add some extra rules to your .htaccess file to cover this situation. For instance, you could use the following to redirect to the www version of each domain name. This example is the same as option 1 above, and simply adds another RewriteCond and RewriteRule to handle the extra domain name.

RewriteEngine On
RewriteCond %{HTTP_HOST}  ^example.com [nocase]
RewriteRule ^(.*)         http://www.example.com/$1 [last,redirect=301]
RewriteCond %{HTTP_HOST}  ^second-domain.com [nocase]
RewriteRule ^(.*)         http://www.second-domain.com/$1 [last,redirect=301]

Both example.com and second-domain.com need to specified since the same .htaccess file will be used by both domain names.

In the example above, requests for "example.com" and "second-domain.com" are redirected to the "www" version of the original requested domain name. If you prefer to always redirect to one particular name, you can specify that name in the appropriate RewriteRule. For example, if you wanted to redirect requests for "example.com", "second-domain.com" and "www.second-domain.com" to "www.example.com", you could modify the above example to look like this instead:

RewriteEngine On
RewriteCond %{HTTP_HOST}  ^example.com [nocase,OR]
RewriteCond %{HTTP_HOST}  ^second-domain.com [nocase,OR]
RewriteCond %{HTTP_HOST}  ^www.second-domain.com [nocase]
RewriteRule ^(.*)         http://www.example.com/$1 [last,redirect=301]

If you just want to force all requests to always use one specific domain name, you can use just one RewriteCond line which matches if the requested domain name is not the desired name:

RewriteEngine On
RewriteCond %{HTTP_HOST}  !^www.example.com [nocase]
RewriteRule ^(.*)         http://www.example.com/$1 [last,redirect=301]

This format is simplest, but also has the largest effect. It affects all domain name variations, including all subdomains (such as “blog.example.com”) and all domain name aliases and their subdomains (such as “second-domain.com”, “www.second-domain.com”, and “other.second-domain.com”). Be sure to test your Web site thoroughly if you use this format.

For more information

Setting up .htaccess rules that use the Apache mod_rewrite module is a complex process. Our htaccess page covers some of the basics, but you might want to do some additional reading. The following pages may be useful: