Rewrite domain.com to www.domain.com using htaccess in Apache

You may want all visitors to your site using www in front of your domain name (www.yourdomain.com) instead of just your domain name (yourdomain.com). By implementing a simple .htaccess RewriteRule, visitors to yourdomain.com will see the URL change in their browser as they are redirected to the correct URL.


To redirect a URL such as “http://yourdomain.com/images/logo.png” to “http://www.yourdomain.com/images/logo.png” use:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=permanent,L]

In the RewriteRule code, the R=permanent sets the type of redirection (permanent is 301; the default is temporary or 302). The L makes this the last Rule to use and stops rewrite processing.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

  • http://www.escorts360.com escorts

    Thanks i will try

  • http://pthree.org Aaron Toponce

    Why? WWW (World Wide Web) was incorporated to force browsers to use the HTTP protocol when the Internet was new. Now all modern browsers default to HTTP by default, so what is the point of WWW? Mail servers certainly don’t require you to use ‘address@mail.yourdomain.com’, so why should the web be any different?

    No, sites should be redirecting the ‘www’ subdomain to the TLD without ‘www’. See http://no-www.org for more info. Class B compliance should be the goal of every site owner. Forcing WWW is just silly and redundant.

    • Anonymous

      YOU are just silly and redundant.

    • Hal_9000_ai

      It’s not silly nor redundant if you are the one running and owning the server and the dedicated IP address running to it. The choice to force content to be served from one NameHost is a valid one, especially if you Host several domains. Anyone that has worked with servers and DNS knows that anyone can point their domain to your Dedicated IP address, and without such technology as mod_rewrite available at the domain root level, someone can essentially server your content under their domain name in a web browser. It is common practise with Domain Harvesters. Anyways, my point is that what is absurd and/or redundant to someone, usually means it the reasoning may be over their head. If we talking about apples and we’re looking at an Macintosh Apple and a Granny Smith Apple, you just see Apples, I see apples and computers and my Grandma Smith. Difference is really, I OWN the apples , while you’re just talkin abot it ;)

    • Hal_9000_ai

      Oh and, if it’s good enough for FACEBOOK.COM which re-routes to http://www.facebook.com, THEN there is obvious reason for it. Let’s see, Aaron Toponce versus $9 Billion Valued Company. Let the masses decide, what is redundant

      • Dominic

        > By FAAAAAAAAAAR the dumbest comment EVER posted on the Internet.

        Maybe facbook did it because they dont want to confuse retards like you. More likely than your approach.

        So, with your – lets call for easy “logic” – logic Hitler was right,. He couldnt be wrong. Millions followed him. You worship Hitler? Not? Why not? Think ybout your post before posting some dumass sh*t.

  • g1smd

    If the idea is to redirect all non-canonical forms of the hostname to exactly http://www.example.com/ beware that your standard code does not redirect www requests with an appended port number http://www.example.com:80/ and does not redirect www requests with an appended period http://www.example.com./ either. This leaves a site open to Duplicate Content indexing if malicious links are pointed at non-canonical URLs.

    The fix is very simple, a minor edit to the originally suggested code:

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

    Additionally, the ( ) ? syntax ensures that HTTP/1.0 requests – which have a blank HOST header – do not cause an infinite redirection loop.

  • bmatthewshea

    Not quite there..
    needs to a match true condition -> “!”:

    RewriteCond %{HTTP_HOST} !^(www.example.com)?$ [NC]

    (added ‘!” and [NC])

  • Gerd Naschenweng

    Thanks for the snippet – works as intended.