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 🙂

  6. Reply March 10, 2016 / 20:46 JaswinderSiteGround Team

    I am hosting my site at

    In the beginning I was having non www and then after a while I changed it to www.

    Is it necessary to put this or not?
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^ [NC]
    RewriteRule ^(.*)$$1 [L,R=301]

    • Reply March 11, 2016 / 11:59 Ivan YordanovSiteGround Team

      Your site is currently redirecting to You don't need to use the .htaccess code, since you are hosting a WordPress application. With WordPress, it is enough just to change the Home and SiteURL to the domain name, including the www prefix.

  7. Reply March 15, 2016 / 18:41 JaswinderSiteGround Team

    Thanks IVAN YORDANOV for replying.

  8. Reply April 9, 2016 / 14:36 Talha RajaSiteGround Team

    I have tried each and every way even place to check code and code is fine and redirecting my site to it but it doesn't work when i do ''
    Too much redirecting loop error comes .

  9. Reply April 9, 2016 / 14:41 Talha RajaSiteGround Team

    Done Done Done 😀
    I was not chaning URL first from setiings of wordpress which was not loading in code . Thanks a lot man . I have spent 2 3 hours on this page lol and was not getting what i am doing wrong but finally i did .
    Thanks 🙂

    • Reply April 11, 2016 / 06:06 Hristo PandjarovSiteGround Team

      Glad you did it 🙂

  10. Reply April 21, 2016 / 00:02 EllaSiteGround Team

    What are the cons/pros of using non-www URLs or www URLs?
    Which one woud you suggest? Site is povered by WordPress. Using SiteGround.

    • Reply April 21, 2016 / 08:02 Hristo PandjarovSiteGround Team

      It's more of a personal preference whether you want to use www. or not. There isn't any difference as long as you stick to one of the options.

  11. Reply April 23, 2016 / 14:43 Sunny UjjawalSiteGround Team

    I tried some codes to redirect http to https ... and non www to www .. homepage is redirecting but not internal pages .. Can you give exact code for htaccess file.
    This is my htaccess file code:

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

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

    • Reply May 3, 2016 / 09:06 escalationsSiteGround Team

      The .htaccess code for the desired redirects would be the following one:

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

  12. Reply May 12, 2016 / 11:27 Ray CassidySiteGround Team

    Just looked at this thread as trying to solve a problem of the root domain not displaying after implementation of https and a change from bigcommerce to wordpress.

    The old website's URL requests redirect perfectly to the https equivalents (many pagename changes so they have had to be done as page by page 301 redirects - no consistent naming patterns in new or old)

    Requests for http version of the new URLs also redirects properly to the https without me having added anything to htaccess. (I assume this has been handled at the lower httpd.conf) level as I haven't had a response from my contractor with the details of his https license.

    However if I request the non-www root domain I get this error
    This site can’t be reached

    domain .com’s server DNS address could not be found.
    Search Google for domain name

    I am just sub contracting some htaccess rewrites and don't have access to all the hosting / domain accounts just now.

    Have any of the readers here experienced this behaviour and does anybody know the root cause?

    Is anybody familiar with this

    • Reply May 12, 2016 / 12:45 Ivan StefanovSiteGround Team

      Ray, if you've not pointed the nameservers of your domain to our server ( and and you've just changed the A record of your or of, then most likely this is the cause of the issue.

      If this is not the case and the DNS settings are correct, please post a support ticket in the respective category and my colleagues will be happy to help you with this issue.

* (Required)