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.

  13. Reply June 1, 2016 / 03:39 BURN-ADDiCTSiteGround Team

    Thank you...
    For about 3 years, I've been using DNS to point my naked domain to www, and I never really understood why it messed with my MX records...

    Since using your .htaccess method, I'm (for the first time ever) able to use email simultenously with my up and running domain.

  14. Reply July 4, 2016 / 14:37 BenSiteGround Team

    How do you do it if you use your htaccess file for multiples website?
    Should I write each line for each website i own?

    Thanks in advance for your response.

    • Reply July 5, 2016 / 11:26 escalationsSiteGround Team

      .htaccess files work recursively for sub-directories. Everything that is written in an .htaccess for parent directory will have effect for all sub-dirs unless the option is redefined in a new .htaccess placed in the respective sub-directory.

      If there is no main directory for your account, an .htaccess file with the respective lines should be present in each directory where you would like it to have effect.

  15. Reply July 14, 2016 / 17:17 RobSiteGround Team

    Hi guy's,
    I can not find how to redirect a url from https://www to just https

    can you tell me how?

    • Reply July 15, 2016 / 07:06 Marina YordanovaSiteGround Team

      Hello Rob, please check out this article

  16. Reply August 19, 2016 / 12:34 sugithaSiteGround Team

    Hi Guys,

    I have a domain from .academy TLD, it works well without www, "" If I include www in browser "" it gives domain search result. I want to do 301 redirection from non www to www,

    Is anyone has experience the same ? any inputs to fix this ?

    • Reply August 23, 2016 / 14:38 Ivan StefanovSiteGround Team

      Sugitha, the domain TLD should not when it comes to www redirections. I suspect that the issue is caused by a domain dependent application that you have installed on your domain and it should be configured to work properly through WWW.

  17. Reply August 30, 2016 / 10:35 M ImranSiteGround Team

    Hi IVAN,
    Should I change the text "" to with my current domain name or just paste the code in .htaccess file as mentioned in the tutorial.

    M Imran

    • Reply August 31, 2016 / 08:04 escalationsSiteGround Team

      You should set your actual domain name instead of ""

  18. Reply September 11, 2016 / 11:01 M ImranSiteGround Team

    The funny thing is, its working fine without entering my domain name. 🙂

    • Reply September 14, 2016 / 13:07 escalationsSiteGround Team

      That should be normal.

  19. Reply September 13, 2016 / 11:27 SachinSiteGround Team


    Non-www to WWW redirection is not working in the case below,


    The above URL should go to custom 404 page. But this is not happened and shows 400 status code.

    If I add WWW, it works fine. How can I solve this?


    • Reply September 14, 2016 / 12:56 escalationsSiteGround Team

      This depends on all RewriteRules in your .htaccess as well as the settings for the application you are using under the domain in question.

  20. Reply September 20, 2016 / 12:48 EhteshamSiteGround Team

    I am trying to redirect whole website to https://www. | http://www. version is redirect to https://www. but non-www version is not redirecting this is the redirection code I've used

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

    • Reply October 4, 2016 / 12:23 Ivan StefanovSiteGround Team

      Ehtesham, sometimes this might be caused by a domain dependent application like WordPress for example, where you need to apply different .htaccess rules, like:

      rewritecond %{SERVER_PORT} 80
      rewriterule ^(.*)$$1 [R,L]
      rewriterule ^index\.php$ - [L]
      rewritecond %{REQUEST_FILENAME} !-f
      rewritecond %{REQUEST_FILENAME} !-d
      rewriterule . /index.php [L]

      If this is not the case and/or the aforementioned code doesn't help, please submit a ticket and we'll be glad to look at the issue.

  21. Reply September 26, 2016 / 22:57 Jemma WiltshireSiteGround Team


    I have tried adding code to both the .htaccess in public_html folder and in sub directory for website. In public folder it didnt do anything. In sub directory it says "Error to many redirects".

    I am not able to change the in wordpress as it is greyed out. The website has a map on homepage and then two wordpress sites connected currently but would like

    Any suggestions?

    • Reply October 4, 2016 / 13:06 Ivan StefanovSiteGround Team

      Jemma, I would suggest you to directly change the site_url and home settings in WordPress options table. If you need more details or help, please submit a support ticket.

  22. Reply October 21, 2016 / 14:44 DavidSiteGround Team

    How can I redirect 'www' to the root domain without specifying the domain? I'm hosting multiple sites from a single MODX install (all in the same folder) and if I create this redirect in .htaccess it redirects everything to the main site. Is it possible to replace the domain name with some regex?

    • Reply October 24, 2016 / 14:52 Ivan StefanovSiteGround Team

      David, you can expand the rewrite conditions or rules to include:
      RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.TLD [NC]
      RewriteRule (.*) http://www.DOMAIN.TLD/$1 [R=301,L]

* (Required)