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]

      • Reply December 1, 2016 / 06:58 SimonSiteGround Team

        Am I mistaken but this would not redirect to ?

        I'm using this but it's a bit suboptimal :

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

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

        • Reply December 1, 2016 / 09:46 Ivan StefanovSiteGround Team

          Simon, if you want to redirect both HTTP and HTTPS non-www Urls to www, you can combine rules as follows:

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

          • February 11, 2017 / 16:35 LeoSiteGround Team

            There is a lot a better way than you two provided.

            RewriteCond %{HTTPS} off
            RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

            RewriteCond %{HTTP_HOST} !^www\. [NC]
            RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

        • Reply December 4, 2016 / 13:48 Luka PaunovićSiteGround Team

          This seems to do the trick

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

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

          • December 4, 2016 / 13:48 Luka PaunovićSiteGround Team

            Haha sorry i mistakenly left my domain, you just replace with your domain of course 🙂

          • March 13, 2017 / 06:05 FacundoSiteGround Team


            I need to redirect non www to www and at the same time force HTTPS. This did it. Thank you. This did it.

  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]

  23. Reply November 2, 2016 / 08:14 Stoyan TunkovSiteGround Team

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

    When i put this into my .htaccess file and i load redirection going to
    Where is my mistake...

    • Reply November 4, 2016 / 08:40 Ivan StefanovSiteGround Team

      Stoyan, such redirect might occur when there is a www subdomain with a document root "/www/root" down the Linux tree. It also depends of the application you have, as some unpredicted behaviour might be observed if you have a domain dependant application out there.

  24. Reply December 1, 2016 / 08:28 seonewmediagroup@gmail.comSiteGround Team

    When i write this code my site is not redirecting to www. can i know what is the issue please?

    • Reply December 1, 2016 / 09:48 Ivan StefanovSiteGround Team

      The issue might be related to the application type and/or hardcoded links in your app files. Please submit a support ticket and my colleagues will be glad to help you with that.

  25. Reply December 8, 2016 / 14:31 clickpulsa.comSiteGround Team

    how about this one.. this good or not??
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

    • Reply December 20, 2016 / 13:53 Ivan StefanovSiteGround Team

      Yes, this is a little bit more general rule, but should be applicable in such cases as well.

  26. Reply December 15, 2016 / 21:03 Reuben GloverSiteGround Team

    Worked Perfectly! Thx

  27. Reply January 4, 2017 / 08:16 krishna saradhiSiteGround Team

    Perfectly Worked..Thanks a lot!!

  28. Reply January 13, 2017 / 12:42 AnnaSiteGround Team


    Can anyone help....

    I would like to redirect all '/' urls to non '/' url's. What I mean by this is for example:

    Point - a link that returns an error if you put '/' on the end of it
    to - the actual link

    Will I have to redirect every single page (this will be time consuming) or is there a quicker way of doing this?
    I need to point all the '/' pages to the non '/' pages

    • Reply January 17, 2017 / 12:10 escalationsSiteGround Team

      First of all in the provided example that would be simply a wrong URL. If you think your website visitors would type wrong URLs you might want to implement a custom 404 Not Found page instead.

  29. Reply January 22, 2017 / 16:24 Greg ColtmanSiteGround Team

    Hi All
    I wonder if you can help. I have a wordpress website.

    Until a few weeks ago it was the non www version. I didnt realise i had set it up as

    I have changed it in wordpress settings, most of my external links point to the www version.

    In google webmaster i have both ww and non www versions and in the site settings, the preferred domain set to www version.

    Do i need to set up a redirect so anyone accessing the non www version is directed to the www version.

    also, in GTmetrix the non www version doesn't render properly, but the www version does, and wierdly, both versions get really high scores??

    Any help would be greatly apreciated

    • Reply February 3, 2017 / 12:51 Ivan AtanasovSiteGround Team

      I see that the non-www version of is now redirecting to without issues. This must mean that you have already reconfigured your app to function with and set up a possible redirect.
      As for the fact that GTmetrix does not render the non-www version of your website - yes, this is the expected result since WordPress is a domain dependant application which will work with the domain name set in it's database.

  30. Reply February 7, 2017 / 05:53 shrikant BeheraSiteGround Team

    Perfectly Worked..Thanks a lot!!

  31. Reply February 9, 2017 / 14:31 ArisSiteGround Team

    Hi there,

    Anyone could help me with my htaccess? my domain is hosted in siteground as well, but I am having an issue and I can't figure it out.

    this is my htaccess for redirection,

    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    Basically, what I want is to redirect HTTP to HTTPS and force to have www on my domain (originally my domain was without the www, e.g., Futhermore, on my code this is what is happening...

    * as is) is NOT redirecting to
    * as is) is NOT redirecting to

    Thanks in advanced!

    • Reply February 9, 2017 / 18:19 Ivan AtanasovSiteGround Team

      You can test with the following redirect:

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

      Where you will change with your actual domain name.

      If you are facing issues with this, I would recommend that you contact us in a Support Ticket since this issue will need to be tested so that we can alter the redirect for you.

  32. Reply March 21, 2017 / 06:14 MyilrajSiteGround Team

    The above codes were working fine, but what would be the code if we need to redirect all incoming links from both www and non www versions to a www version of a particular php page.

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

    Here there is "$1" at the end///////

    myilraj G

    • Reply March 22, 2017 / 12:28 Ivan AtanasovSiteGround Team

      If you wish to land the visitors of and on, the following code should work:

      RewriteCond %{HTTP_HOST} ^mywebsite\.in$ [OR]
      RewriteCond %{HTTP_HOST} ^www\.mywebsite\.in$
      RewriteRule ^/?$ "http\:\/\/www\.mywebsite\.in\/my-first-page.php" [R=301,L]

  33. Reply April 5, 2017 / 09:40 MichaelSiteGround Team

    Should I change my canonical links to include www as well?

    Is there any SEO or other impact if I don't?

    • Reply April 5, 2017 / 12:29 MichaelSiteGround Team

      Just realised the canonical URLs change automatically.

      But I still wonder, is there any impact on the SEO side of it?

      • Reply April 6, 2017 / 12:19 Ivan AtanasovSiteGround Team

        This should not matter for your SEO score. Google are saying that this is your preference, which means that you can set your domain URL in Google Webmaster Tools and they will honor it.

        This is a bit more detailed explanation.

* (Required)