Redirecting non-www to www with .htaccess

If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:

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

This will redirect any requests to http://my-domain.com to http://www.my-domain.com. There are several benefits from doing that:

  • It will avoid duplicate content in Google
  • It will avoid the possibility of split page rank and/or split link popularity (inbound links).
  • It's nicer, and more consistent.

Note that if your site has already been indexed by Google without the www, this might cause unwanted side effects, like lost of PR. I don't think this would happen, or in any case it would be a temporary issue (we are doing a permanent redirect, 301, so Google should transfer all rankings to the www version). But anyway, use at your own risk!

Something nice about the code above is that you can use it for any website, since it doesn't include the actual domain name.

Redirecting www to non-www

If you want to do the opposite, the code is very similar:

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

In this case we are explicitly typing the domain name. I'm sure it's possible to do it in a generic way, but I haven't had the time to work one out and test it. So remember to change 'my-domain' with your domain name!

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, ortrackback from your own site.

154 comments to “Redirecting non-www to www with .htaccess”

  1. #01 By natalia, 080313 at 21:03

    Would you mind explaining exactly what each part of those two lines does? :)
    Always been curious.

  2. #02 By dense13, 080313 at 22:15

    Sure! The first line sets a condition: only if the condition is true, the second line will be processed. The condition could be 'translated' to: "if the host name doesn't start with www.". The regular expression !^www\. means this:

    ! = not
    ^ = start
    \. = . (the backslash is the escape character, because dots have a special meaning in regular expressions, and therefore must be escaped)

    So !^www\. means "doesn't start with www.".

    The second line is the actual rewrite rule: again it uses regular expressions to match certain urls, and then rewrites them to something else. The first part is the regular expression:

    ^(.*)$

    This means: anything! You already know the ^ sign. The (.*) bit means zero or more characters (the dot means any character, the asterisk means zero or more). The final $ means 'end'.

    Then comes the bit that says how to rewrite the url:

    http://www.%{HTTP_HOST}/$1 [R=301,L]

    %{HTTP_HOST} will be replaced by the host name (i.e. dense13.com).
    $1 references whatever was matched in the regular expression between the brackets, which in this case is everything.
    The [R=301,L] means "inform the user agent that this is a permanent redirect (HTTP 301 code), and don't process any more rewrite rules (if there were any after this one).

    If you're not familiar with regular expressions, this might still look a bit abstract, feel free to ask for more details. :)

本文地址:http://blog.csdn.net/aerchi/article/details/40976651

个人网站:http://www.aerchi.com

你可能感兴趣的:(Redirecting non-www to www with .htaccess)