.htaccess - how to force “www.” in a generic way?












95















This will change domain.com to www.domain.com:



# Force the "www."
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]


How do I replace the "domain" part so that this works on any domain?










share|improve this question



























    95















    This will change domain.com to www.domain.com:



    # Force the "www."
    RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]


    How do I replace the "domain" part so that this works on any domain?










    share|improve this question

























      95












      95








      95


      63






      This will change domain.com to www.domain.com:



      # Force the "www."
      RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
      RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]


      How do I replace the "domain" part so that this works on any domain?










      share|improve this question














      This will change domain.com to www.domain.com:



      # Force the "www."
      RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
      RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]


      How do I replace the "domain" part so that this works on any domain?







      .htaccess mod-rewrite






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 6 '11 at 21:21









      StackOverflowNewbieStackOverflowNewbie

      13.8k81226381




      13.8k81226381
























          8 Answers
          8






          active

          oldest

          votes


















          231














          I would use this rule:



          RewriteEngine On
          RewriteCond %{HTTP_HOST} !=""
          RewriteCond %{HTTP_HOST} !^www. [NC]
          RewriteCond %{HTTPS}s ^on(s)|
          RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


          The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.; the third checks for HTTPS (%{HTTPS} is either on or off, so %{HTTPS}s is either ons or offs and in case of ons the s is matched). The substitution part of RewriteRule then just merges the information parts to a full URL.






          share|improve this answer





















          • 25





            This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be: RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

            – Jared Pomranky
            May 14 '13 at 19:04








          • 2





            I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...

            – lol
            Jul 18 '13 at 23:21






          • 3





            very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant

            – code_monk
            Feb 22 '14 at 21:31











          • Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.

            – Clain Dsilva
            Mar 3 '15 at 5:27






          • 1





            Thanks! I would add RewriteEngine On as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.

            – Ryan Casas
            Oct 7 '15 at 16:50



















          40














          This will do it:



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





          share|improve this answer


























          • Simple, and effective!

            – ChaseMoskal
            Apr 30 '14 at 8:44






          • 1





            Thanks, but what about https:// ?

            – Lucas Bustamante
            Sep 26 '17 at 19:21



















          4














          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]





          share|improve this answer































            3














            This won't work with subdomains.



            domain.com correctly gets redirected to www.domain.com



            but



            images.domain.com gets redirected to www.images.domain.com



            Instead of checking if the subdomain is "not www", check if there are two dots:



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





            share|improve this answer





















            • 8





              This won't work with certain TLDs, like .co.uk

              – kieranajp
              Dec 11 '13 at 11:07











            • I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?

              – TylersSN
              Mar 12 '15 at 13:57






            • 1





              @iUseMagentoNow just increase the dots, for example: RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC] (Strictly speaking, your TLD is still .uk and your domain is something.co.)

              – mirabilos
              Dec 15 '15 at 14:17











            • .co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.

              – Chris
              Feb 4 '16 at 21:43



















            1














            The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.



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





            share|improve this answer































              1














              RewriteEngine On

              RewriteCond %{HTTP_HOST} ^[^.]+.[^.]+$
              RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]


              This redirects example.com to www.example.com excluding subdomains.






              share|improve this answer


























              • For me this seems to be sending www.example.com to www.www.example.com haha

                – Albert Renshaw
                Jan 25 '17 at 5:41











              • @Albert Perhaps this is because of your browser cache. Clear your cache and Retry.

                – starkeen
                Jan 25 '17 at 6:04











              • You are correct! I just ran in incognito (chrome) and it worked fine, thanks!

                – Albert Renshaw
                Jan 25 '17 at 6:37






              • 1





                I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?

                – Sacha Vorbeck
                Jul 3 '17 at 9:13






              • 1





                For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds

                – Sacha Vorbeck
                Jul 3 '17 at 16:36



















              0














              This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)






              share|improve this answer































                -1














                this worked like magic for me




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







                share|improve this answer






















                  protected by starkeen Dec 15 '16 at 16:50



                  Thank you for your interest in this question.
                  Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                  Would you like to answer one of these unanswered questions instead?














                  8 Answers
                  8






                  active

                  oldest

                  votes








                  8 Answers
                  8






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  231














                  I would use this rule:



                  RewriteEngine On
                  RewriteCond %{HTTP_HOST} !=""
                  RewriteCond %{HTTP_HOST} !^www. [NC]
                  RewriteCond %{HTTPS}s ^on(s)|
                  RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


                  The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.; the third checks for HTTPS (%{HTTPS} is either on or off, so %{HTTPS}s is either ons or offs and in case of ons the s is matched). The substitution part of RewriteRule then just merges the information parts to a full URL.






                  share|improve this answer





















                  • 25





                    This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be: RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

                    – Jared Pomranky
                    May 14 '13 at 19:04








                  • 2





                    I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...

                    – lol
                    Jul 18 '13 at 23:21






                  • 3





                    very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant

                    – code_monk
                    Feb 22 '14 at 21:31











                  • Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.

                    – Clain Dsilva
                    Mar 3 '15 at 5:27






                  • 1





                    Thanks! I would add RewriteEngine On as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.

                    – Ryan Casas
                    Oct 7 '15 at 16:50
















                  231














                  I would use this rule:



                  RewriteEngine On
                  RewriteCond %{HTTP_HOST} !=""
                  RewriteCond %{HTTP_HOST} !^www. [NC]
                  RewriteCond %{HTTPS}s ^on(s)|
                  RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


                  The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.; the third checks for HTTPS (%{HTTPS} is either on or off, so %{HTTPS}s is either ons or offs and in case of ons the s is matched). The substitution part of RewriteRule then just merges the information parts to a full URL.






                  share|improve this answer





















                  • 25





                    This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be: RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

                    – Jared Pomranky
                    May 14 '13 at 19:04








                  • 2





                    I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...

                    – lol
                    Jul 18 '13 at 23:21






                  • 3





                    very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant

                    – code_monk
                    Feb 22 '14 at 21:31











                  • Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.

                    – Clain Dsilva
                    Mar 3 '15 at 5:27






                  • 1





                    Thanks! I would add RewriteEngine On as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.

                    – Ryan Casas
                    Oct 7 '15 at 16:50














                  231












                  231








                  231







                  I would use this rule:



                  RewriteEngine On
                  RewriteCond %{HTTP_HOST} !=""
                  RewriteCond %{HTTP_HOST} !^www. [NC]
                  RewriteCond %{HTTPS}s ^on(s)|
                  RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


                  The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.; the third checks for HTTPS (%{HTTPS} is either on or off, so %{HTTPS}s is either ons or offs and in case of ons the s is matched). The substitution part of RewriteRule then just merges the information parts to a full URL.






                  share|improve this answer















                  I would use this rule:



                  RewriteEngine On
                  RewriteCond %{HTTP_HOST} !=""
                  RewriteCond %{HTTP_HOST} !^www. [NC]
                  RewriteCond %{HTTPS}s ^on(s)|
                  RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


                  The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.; the third checks for HTTPS (%{HTTPS} is either on or off, so %{HTTPS}s is either ons or offs and in case of ons the s is matched). The substitution part of RewriteRule then just merges the information parts to a full URL.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 24 '18 at 12:38









                  Max Vollmer

                  5,73651737




                  5,73651737










                  answered Feb 10 '11 at 15:13









                  GumboGumbo

                  506k90671759




                  506k90671759








                  • 25





                    This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be: RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

                    – Jared Pomranky
                    May 14 '13 at 19:04








                  • 2





                    I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...

                    – lol
                    Jul 18 '13 at 23:21






                  • 3





                    very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant

                    – code_monk
                    Feb 22 '14 at 21:31











                  • Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.

                    – Clain Dsilva
                    Mar 3 '15 at 5:27






                  • 1





                    Thanks! I would add RewriteEngine On as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.

                    – Ryan Casas
                    Oct 7 '15 at 16:50














                  • 25





                    This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be: RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

                    – Jared Pomranky
                    May 14 '13 at 19:04








                  • 2





                    I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...

                    – lol
                    Jul 18 '13 at 23:21






                  • 3





                    very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant

                    – code_monk
                    Feb 22 '14 at 21:31











                  • Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.

                    – Clain Dsilva
                    Mar 3 '15 at 5:27






                  • 1





                    Thanks! I would add RewriteEngine On as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.

                    – Ryan Casas
                    Oct 7 '15 at 16:50








                  25




                  25





                  This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be: RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

                  – Jared Pomranky
                  May 14 '13 at 19:04







                  This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be: RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

                  – Jared Pomranky
                  May 14 '13 at 19:04






                  2




                  2





                  I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...

                  – lol
                  Jul 18 '13 at 23:21





                  I don't get any of my subdomains forwarded to www.mydomain.com using the rule posted in the question...

                  – lol
                  Jul 18 '13 at 23:21




                  3




                  3





                  very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant

                  – code_monk
                  Feb 22 '14 at 21:31





                  very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant

                  – code_monk
                  Feb 22 '14 at 21:31













                  Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.

                  – Clain Dsilva
                  Mar 3 '15 at 5:27





                  Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm.

                  – Clain Dsilva
                  Mar 3 '15 at 5:27




                  1




                  1





                  Thanks! I would add RewriteEngine On as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.

                  – Ryan Casas
                  Oct 7 '15 at 16:50





                  Thanks! I would add RewriteEngine On as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is.

                  – Ryan Casas
                  Oct 7 '15 at 16:50













                  40














                  This will do it:



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





                  share|improve this answer


























                  • Simple, and effective!

                    – ChaseMoskal
                    Apr 30 '14 at 8:44






                  • 1





                    Thanks, but what about https:// ?

                    – Lucas Bustamante
                    Sep 26 '17 at 19:21
















                  40














                  This will do it:



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





                  share|improve this answer


























                  • Simple, and effective!

                    – ChaseMoskal
                    Apr 30 '14 at 8:44






                  • 1





                    Thanks, but what about https:// ?

                    – Lucas Bustamante
                    Sep 26 '17 at 19:21














                  40












                  40








                  40







                  This will do it:



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





                  share|improve this answer















                  This will do it:



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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 6 '18 at 0:46









                  Wade

                  2,60821938




                  2,60821938










                  answered Feb 6 '11 at 21:36









                  Martin DrapeauMartin Drapeau

                  1,2521314




                  1,2521314













                  • Simple, and effective!

                    – ChaseMoskal
                    Apr 30 '14 at 8:44






                  • 1





                    Thanks, but what about https:// ?

                    – Lucas Bustamante
                    Sep 26 '17 at 19:21



















                  • Simple, and effective!

                    – ChaseMoskal
                    Apr 30 '14 at 8:44






                  • 1





                    Thanks, but what about https:// ?

                    – Lucas Bustamante
                    Sep 26 '17 at 19:21

















                  Simple, and effective!

                  – ChaseMoskal
                  Apr 30 '14 at 8:44





                  Simple, and effective!

                  – ChaseMoskal
                  Apr 30 '14 at 8:44




                  1




                  1





                  Thanks, but what about https:// ?

                  – Lucas Bustamante
                  Sep 26 '17 at 19:21





                  Thanks, but what about https:// ?

                  – Lucas Bustamante
                  Sep 26 '17 at 19:21











                  4














                  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]





                  share|improve this answer




























                    4














                    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]





                    share|improve this answer


























                      4












                      4








                      4







                      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]





                      share|improve this answer













                      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]






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 17 '14 at 9:13









                      ClickForWebsClickForWebs

                      493




                      493























                          3














                          This won't work with subdomains.



                          domain.com correctly gets redirected to www.domain.com



                          but



                          images.domain.com gets redirected to www.images.domain.com



                          Instead of checking if the subdomain is "not www", check if there are two dots:



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





                          share|improve this answer





















                          • 8





                            This won't work with certain TLDs, like .co.uk

                            – kieranajp
                            Dec 11 '13 at 11:07











                          • I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?

                            – TylersSN
                            Mar 12 '15 at 13:57






                          • 1





                            @iUseMagentoNow just increase the dots, for example: RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC] (Strictly speaking, your TLD is still .uk and your domain is something.co.)

                            – mirabilos
                            Dec 15 '15 at 14:17











                          • .co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.

                            – Chris
                            Feb 4 '16 at 21:43
















                          3














                          This won't work with subdomains.



                          domain.com correctly gets redirected to www.domain.com



                          but



                          images.domain.com gets redirected to www.images.domain.com



                          Instead of checking if the subdomain is "not www", check if there are two dots:



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





                          share|improve this answer





















                          • 8





                            This won't work with certain TLDs, like .co.uk

                            – kieranajp
                            Dec 11 '13 at 11:07











                          • I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?

                            – TylersSN
                            Mar 12 '15 at 13:57






                          • 1





                            @iUseMagentoNow just increase the dots, for example: RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC] (Strictly speaking, your TLD is still .uk and your domain is something.co.)

                            – mirabilos
                            Dec 15 '15 at 14:17











                          • .co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.

                            – Chris
                            Feb 4 '16 at 21:43














                          3












                          3








                          3







                          This won't work with subdomains.



                          domain.com correctly gets redirected to www.domain.com



                          but



                          images.domain.com gets redirected to www.images.domain.com



                          Instead of checking if the subdomain is "not www", check if there are two dots:



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





                          share|improve this answer















                          This won't work with subdomains.



                          domain.com correctly gets redirected to www.domain.com



                          but



                          images.domain.com gets redirected to www.images.domain.com



                          Instead of checking if the subdomain is "not www", check if there are two dots:



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






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 24 '13 at 3:07

























                          answered Nov 24 '13 at 3:02









                          john mccarthyjohn mccarthy

                          312




                          312








                          • 8





                            This won't work with certain TLDs, like .co.uk

                            – kieranajp
                            Dec 11 '13 at 11:07











                          • I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?

                            – TylersSN
                            Mar 12 '15 at 13:57






                          • 1





                            @iUseMagentoNow just increase the dots, for example: RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC] (Strictly speaking, your TLD is still .uk and your domain is something.co.)

                            – mirabilos
                            Dec 15 '15 at 14:17











                          • .co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.

                            – Chris
                            Feb 4 '16 at 21:43














                          • 8





                            This won't work with certain TLDs, like .co.uk

                            – kieranajp
                            Dec 11 '13 at 11:07











                          • I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?

                            – TylersSN
                            Mar 12 '15 at 13:57






                          • 1





                            @iUseMagentoNow just increase the dots, for example: RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC] (Strictly speaking, your TLD is still .uk and your domain is something.co.)

                            – mirabilos
                            Dec 15 '15 at 14:17











                          • .co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.

                            – Chris
                            Feb 4 '16 at 21:43








                          8




                          8





                          This won't work with certain TLDs, like .co.uk

                          – kieranajp
                          Dec 11 '13 at 11:07





                          This won't work with certain TLDs, like .co.uk

                          – kieranajp
                          Dec 11 '13 at 11:07













                          I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?

                          – TylersSN
                          Mar 12 '15 at 13:57





                          I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk?

                          – TylersSN
                          Mar 12 '15 at 13:57




                          1




                          1





                          @iUseMagentoNow just increase the dots, for example: RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC] (Strictly speaking, your TLD is still .uk and your domain is something.co.)

                          – mirabilos
                          Dec 15 '15 at 14:17





                          @iUseMagentoNow just increase the dots, for example: RewriteCond %{HTTP_HOST} !^(.*).(.*).(.*). [NC] (Strictly speaking, your TLD is still .uk and your domain is something.co.)

                          – mirabilos
                          Dec 15 '15 at 14:17













                          .co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.

                          – Chris
                          Feb 4 '16 at 21:43





                          .co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view.

                          – Chris
                          Feb 4 '16 at 21:43











                          1














                          The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.



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





                          share|improve this answer




























                            1














                            The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.



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





                            share|improve this answer


























                              1












                              1








                              1







                              The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.



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





                              share|improve this answer













                              The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.



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






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Feb 6 '11 at 21:40









                              RoUSRoUS

                              854625




                              854625























                                  1














                                  RewriteEngine On

                                  RewriteCond %{HTTP_HOST} ^[^.]+.[^.]+$
                                  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]


                                  This redirects example.com to www.example.com excluding subdomains.






                                  share|improve this answer


























                                  • For me this seems to be sending www.example.com to www.www.example.com haha

                                    – Albert Renshaw
                                    Jan 25 '17 at 5:41











                                  • @Albert Perhaps this is because of your browser cache. Clear your cache and Retry.

                                    – starkeen
                                    Jan 25 '17 at 6:04











                                  • You are correct! I just ran in incognito (chrome) and it worked fine, thanks!

                                    – Albert Renshaw
                                    Jan 25 '17 at 6:37






                                  • 1





                                    I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?

                                    – Sacha Vorbeck
                                    Jul 3 '17 at 9:13






                                  • 1





                                    For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds

                                    – Sacha Vorbeck
                                    Jul 3 '17 at 16:36
















                                  1














                                  RewriteEngine On

                                  RewriteCond %{HTTP_HOST} ^[^.]+.[^.]+$
                                  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]


                                  This redirects example.com to www.example.com excluding subdomains.






                                  share|improve this answer


























                                  • For me this seems to be sending www.example.com to www.www.example.com haha

                                    – Albert Renshaw
                                    Jan 25 '17 at 5:41











                                  • @Albert Perhaps this is because of your browser cache. Clear your cache and Retry.

                                    – starkeen
                                    Jan 25 '17 at 6:04











                                  • You are correct! I just ran in incognito (chrome) and it worked fine, thanks!

                                    – Albert Renshaw
                                    Jan 25 '17 at 6:37






                                  • 1





                                    I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?

                                    – Sacha Vorbeck
                                    Jul 3 '17 at 9:13






                                  • 1





                                    For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds

                                    – Sacha Vorbeck
                                    Jul 3 '17 at 16:36














                                  1












                                  1








                                  1







                                  RewriteEngine On

                                  RewriteCond %{HTTP_HOST} ^[^.]+.[^.]+$
                                  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]


                                  This redirects example.com to www.example.com excluding subdomains.






                                  share|improve this answer















                                  RewriteEngine On

                                  RewriteCond %{HTTP_HOST} ^[^.]+.[^.]+$
                                  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]


                                  This redirects example.com to www.example.com excluding subdomains.







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Jan 25 '17 at 5:39









                                  Albert Renshaw

                                  9,1941368152




                                  9,1941368152










                                  answered Jul 1 '16 at 4:12









                                  starkeenstarkeen

                                  29.9k145878




                                  29.9k145878













                                  • For me this seems to be sending www.example.com to www.www.example.com haha

                                    – Albert Renshaw
                                    Jan 25 '17 at 5:41











                                  • @Albert Perhaps this is because of your browser cache. Clear your cache and Retry.

                                    – starkeen
                                    Jan 25 '17 at 6:04











                                  • You are correct! I just ran in incognito (chrome) and it worked fine, thanks!

                                    – Albert Renshaw
                                    Jan 25 '17 at 6:37






                                  • 1





                                    I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?

                                    – Sacha Vorbeck
                                    Jul 3 '17 at 9:13






                                  • 1





                                    For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds

                                    – Sacha Vorbeck
                                    Jul 3 '17 at 16:36



















                                  • For me this seems to be sending www.example.com to www.www.example.com haha

                                    – Albert Renshaw
                                    Jan 25 '17 at 5:41











                                  • @Albert Perhaps this is because of your browser cache. Clear your cache and Retry.

                                    – starkeen
                                    Jan 25 '17 at 6:04











                                  • You are correct! I just ran in incognito (chrome) and it worked fine, thanks!

                                    – Albert Renshaw
                                    Jan 25 '17 at 6:37






                                  • 1





                                    I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?

                                    – Sacha Vorbeck
                                    Jul 3 '17 at 9:13






                                  • 1





                                    For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds

                                    – Sacha Vorbeck
                                    Jul 3 '17 at 16:36

















                                  For me this seems to be sending www.example.com to www.www.example.com haha

                                  – Albert Renshaw
                                  Jan 25 '17 at 5:41





                                  For me this seems to be sending www.example.com to www.www.example.com haha

                                  – Albert Renshaw
                                  Jan 25 '17 at 5:41













                                  @Albert Perhaps this is because of your browser cache. Clear your cache and Retry.

                                  – starkeen
                                  Jan 25 '17 at 6:04





                                  @Albert Perhaps this is because of your browser cache. Clear your cache and Retry.

                                  – starkeen
                                  Jan 25 '17 at 6:04













                                  You are correct! I just ran in incognito (chrome) and it worked fine, thanks!

                                  – Albert Renshaw
                                  Jan 25 '17 at 6:37





                                  You are correct! I just ran in incognito (chrome) and it worked fine, thanks!

                                  – Albert Renshaw
                                  Jan 25 '17 at 6:37




                                  1




                                  1





                                  I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?

                                  – Sacha Vorbeck
                                  Jul 3 '17 at 9:13





                                  I like this generic rule very much. But it doesn`t work for domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well?

                                  – Sacha Vorbeck
                                  Jul 3 '17 at 9:13




                                  1




                                  1





                                  For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds

                                  – Sacha Vorbeck
                                  Jul 3 '17 at 16:36





                                  For .co.uk and other sub cctlds this works: ^([^.]+.[^.]+)(.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds

                                  – Sacha Vorbeck
                                  Jul 3 '17 at 16:36











                                  0














                                  This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)






                                  share|improve this answer




























                                    0














                                    This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)






                                      share|improve this answer













                                      This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jul 12 '18 at 22:26









                                      Rick HellewellRick Hellewell

                                      299216




                                      299216























                                          -1














                                          this worked like magic for me




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







                                          share|improve this answer




























                                            -1














                                            this worked like magic for me




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







                                            share|improve this answer


























                                              -1












                                              -1








                                              -1







                                              this worked like magic for me




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







                                              share|improve this answer













                                              this worked like magic for me




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








                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 7 '17 at 9:26









                                              SamYahSamYah

                                              62559




                                              62559

















                                                  protected by starkeen Dec 15 '16 at 16:50



                                                  Thank you for your interest in this question.
                                                  Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                  Would you like to answer one of these unanswered questions instead?



                                                  Popular posts from this blog

                                                  A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                                  Calculate evaluation metrics using cross_val_predict sklearn

                                                  Insert data from modal to MySQL (multiple modal on website)