How to redirect non-www URLs to www?

In order to redirect all of the requests for to, you should set the appropriate rewrite rule. This can be done by adding the following lines at the beginning of the .htaccess file in your public_html folder:


RewriteEngine On
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

From now on, when someone accesses s/he will be redirected to


Using this method is safe because it should not create any redirection loops or interfere with other rewrite rules.


  1. Reply May 30, 2015 / 05:17 Dr. Larry GroopSiteGround Team

    Put the code in and got a redirect loop error.....

    • Reply May 31, 2015 / 14:07 Hristo PandjarovSiteGround Team

      Check out the other rules in your .htaccess file, probably it's interfering with some of the other redirects you probably have.

      • Reply June 23, 2015 / 12:53 NHSiteGround Team

        ... it should not ... interfere with other rewrite rules.

        • Reply June 25, 2015 / 09:27 Hristo PandjarovSiteGround Team

          Fixed, thanks :)

  2. Reply December 19, 2015 / 17:57 MichalSiteGround Team

    RewriteCond %{HTTP_HOST} ^ [NC]
    RewriteRule ^(.*)$$1 [L,R=301]

    RewriteCond %{HTTP_HOST} ^ [NC]
    RewriteRule ^(.*)$$1 [L,R=301]

    Now I may use other domain in the same hosting and it looks like new one.
    Work perfectly!

  3. Reply December 31, 2015 / 16:26 Charlie SasserSiteGround Team

    If you are running WordPress Site from a subdirectory to keep things clean in the root, do you need to update the code in *both* the .htaccess file in the root and the .htaccess in the subdirectory?

    I have tried it both ways and everything seems to work fine as long as at least the root .htaccess has the redirect code.

    The redirect was actually working before I modified the .htaccess code reference here but it seems to be faster since I included code.

    What was doing the redirecting before I added the .htaccess code ?

    PS: I install WP with the WP url set to "" and Site url set to ""

    All of this in prep for running Cloudflare. 😉

    • Reply January 4, 2016 / 10:47 Ivan YordanovSiteGround Team

      Its enough to update one of the .htaccess files - in the root or in the subdirectory. Since the domain name will be specified in the rule, it will be working fine even if it is placed only in the root.

      This article provides information about the redirection in general. Specifically for WordPress, you don't need to make an .htaccess redirect, the application does that itself. Its enough to update the siteURL, home and the database entries (and the link hardcoded in your files, if there are such) to contain the www prefix and your site will be working properly with it.

      PS: When you install WP in subdirectory and want the site to be opened with, you need to set both the home and the siteurl to:

  4. Reply January 17, 2016 / 12:38 fumelineSiteGround Team

    hi, how to do it in a blogger/ blogspot blog? Thanks

    • Reply January 18, 2016 / 07:26 Hristo PandjarovSiteGround Team

      I don't think you have that kind of access to your site on those platforms.

  5. Reply January 27, 2016 / 01:10 ken sSiteGround Team

    im getting a redirect loop here is my htaccess file
    # BEGIN WordPress
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^ [NC]
    RewriteRule ^(.*)$$1 [L,R=301]

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
    RewriteRule .* – [F,L]

    # Set XSS Protection header
    Header set X-XSS-Protection "1; mode=block"

    # Enable expirations
    ExpiresActive On
    # Default directive
    ExpiresDefault "access plus 1 month"
    # My favicon
    ExpiresByType image/x-icon "access plus 1 year"
    # Images
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    # CSS
    ExpiresByType text/css "access plus 1 month"
    # Javascript
    ExpiresByType application/javascript "access plus 1 year"

    ServerSignature Off

    # END WordPress

    • Reply January 27, 2016 / 01:16 ken sSiteGround Team

      nevermind read the post above about the wordpress urls and it fixed it

      • Reply January 27, 2016 / 09:43 Hristo PandjarovSiteGround Team

        Glad you solved it :)

* (Required)