How to Redirect from Your Root Domain to the WWW Subdomain

Wednesday, February 17, 2010 2:14
Posted in category PHP

How to Redirect from Your Root Domain to the WWW Subdomain and Vice Versa Using mod_rewrite?

Most websites stick to one form of their domain name when they refer to their own site, be it the plain domain name, like example.com, or the www form, like www.example.com. Unfortunately others linking to you don’t always follow your preferred style. If some sites link to you with just the domain name, and others link using the www subdomain, then your site may face issues with the search engines. This article shows you how you can work around the problem by automatically redirecting all URL requests of one form to the other in a search engine friendly way.

To redirect (say) example.com to www.example.com, add the following code to your .htaccess file.

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

The above code causes the server to check that the hostname in the URL is example.com. If it is, the visitor will be sent to www.example.com instead.

If you also own, say example.co.uk, example.de and/or some other domain, and want them to be redirected to www.example.com as well, you may prefer to use the following code instead:

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

To redirect any URL starting with (say) www.example.com to example.com, add the following code to your .htaccess file.

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

The above rules checks if the hostname in a URL contains www.example.com. If it does, the visitor is sent to example.com instead.

To use the rule, change all instances of "example.com" to your actual domain name. For the line beginning with RewriteCond, add a backslash ("\") before each of the full stops (or "periods") in your domain name.

You can leave a response, or trackback from your own site.

17 Responses to “How to Redirect from Your Root Domain to the WWW Subdomain”

  1. Url Redirect says:

    February 18th, 2010 at 8:23 pm

    You can set up subdomains from the Domains menu tab. Url Redirect

  2. loans says:

    March 9th, 2010 at 4:17 am

    I want to thank the blogger very much not only for this post but also for his all previous efforts. I found blog.altmint.com to be very interesting. I will be coming back to blog.altmint.com for more information.

  3. Grayfox says:

    March 10th, 2010 at 10:42 pm

    I didn’t understand the concluding part of your article, could you please explain it more?

  4. admin says:

    March 17th, 2010 at 12:54 am

    the concluding part is, if your url contains “www” element, it will delete that “www” part and redirect to “example.com” instead of “www.example.com”

  5. ChorieGoRkike says:

    March 21st, 2010 at 8:56 am

    i surely love your own posting taste, very charming,
    don’t quit as well as keep creating because it just simply nicely to read it.
    impatient to see way more of your well written articles, good bye :)

  6. Vicente Crutchfield says:

    March 29th, 2010 at 7:51 pm

    Just thought i would comment and say neat theme, did you code it yourself? Looks great. Keep it going! If you liketo exchange the links with us please let me know.

  7. PiterJankovich says:

    March 31st, 2010 at 8:32 am

    My name is Piter Jankovich. oOnly want to tell, that your blog is really cool
    And want to ask you: is this blog your hobby?
    P.S. Sorry for my bad english

  8. AttiliSadedal says:

    April 4th, 2010 at 7:02 am

    i came across, lovely web-site
    a way to get blog posts which smash in people:

  9. Abdul Kristen says:

    April 7th, 2010 at 8:15 am

    Take a look at this site I will definetly use it when we have an outsourcing project.

  10. Free sample says:

    April 9th, 2010 at 1:13 am

    I really want to thank the author of this blog for a good article you wrote. Thanks.

  11. WP Themes says:

    April 9th, 2010 at 1:28 am

    Good dispatch and this enter helped me alot in my college assignement. Thanks you for your information.

  12. Palmer Rumschlag says:

    April 12th, 2010 at 1:14 pm

    Hey there, when browsing at your site i see some sort of weird codes all over the page, in case it’s important I just thought I’d let you know it says this with all sorts of other stuff after it: “Warning: Cannot modify header information – headers already sent in wp-settings.php line 12″

  13. la martina says:

    April 13th, 2010 at 6:51 am

    I inaugurate this despatch to be very useful. I am using it in a report I am calligraphy at college.

  14. Dolly says:

    April 13th, 2010 at 2:33 pm

    ЎIncreнble! No estб claro para mн, їcуmo offen que la actualizaciуn de su nombre de blog.altmint.com.
    Have a nice day

  15. Personāla atlase says:

    April 13th, 2010 at 3:08 pm

    You have to express more your opinion to attract more readers, because just a video or plain text without any personal approach is not that valuable. But it is just form my point of view

  16. Josh says:

    April 13th, 2010 at 7:48 pm

    Amazing Dude, I never new this, much appreciated.

  17. composite roof shingles says:

    April 15th, 2010 at 10:38 pm

    When one conceives the topicat hand, i have to be in agreementwith your conclusions. You clearlyshow knowledge about this topic and i have much to find out after reading your article.Much salutations and i will come back for any further updates.

Leave a Reply