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

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: Apache web server  

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.

 

8 Comments -


  1. escorts said on January 4, 2009

    Thanks i will try

  2. Aaron Toponce said on February 13, 2009

    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.

  3. Anonymous said on February 25, 2010

    YOU are just silly and redundant.

  4. g1smd said on May 3, 2010

    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.

  5. bmatthewshea said on April 4, 2011

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

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

    (added ‘!” and [NC])

  6. Hal_9000_ai said on May 13, 2011

    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 ;)

  7. Hal_9000_ai said on May 13, 2011

    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

  8. Gerd Naschenweng said on January 4, 2012

    Thanks for the snippet – works as intended.

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -