http to https apache redirection












128















Environment Centos with apache



Trying to setup automatic redirection from http to https



From manage.mydomain.com --- To ---> https://manage.mydomain.com 


I have tried adding the following to my httpd.conf but it didn't work



 RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]


Any ideas?










share|improve this question



























    128















    Environment Centos with apache



    Trying to setup automatic redirection from http to https



    From manage.mydomain.com --- To ---> https://manage.mydomain.com 


    I have tried adding the following to my httpd.conf but it didn't work



     RewriteEngine on
    ReWriteCond %{SERVER_PORT} !^443$
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]


    Any ideas?










    share|improve this question

























      128












      128








      128


      50






      Environment Centos with apache



      Trying to setup automatic redirection from http to https



      From manage.mydomain.com --- To ---> https://manage.mydomain.com 


      I have tried adding the following to my httpd.conf but it didn't work



       RewriteEngine on
      ReWriteCond %{SERVER_PORT} !^443$
      RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]


      Any ideas?










      share|improve this question














      Environment Centos with apache



      Trying to setup automatic redirection from http to https



      From manage.mydomain.com --- To ---> https://manage.mydomain.com 


      I have tried adding the following to my httpd.conf but it didn't work



       RewriteEngine on
      ReWriteCond %{SERVER_PORT} !^443$
      RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]


      Any ideas?







      linux apache webserver






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 24 '13 at 19:23









      DeanoDeano

      4,637103974




      4,637103974
























          10 Answers
          10






          active

          oldest

          votes


















          189














          I have actually followed this example and it worked for me :)



          NameVirtualHost *:80
          <VirtualHost *:80>
          ServerName mysite.example.com
          DocumentRoot /usr/local/apache2/htdocs
          Redirect permanent / https://mysite.example.com/
          </VirtualHost>

          <VirtualHost _default_:443>
          ServerName mysite.example.com
          DocumentRoot /usr/local/apache2/htdocs
          SSLEngine On
          # etc...
          </VirtualHost>


          Then do:



          /etc/init.d/httpd restart






          share|improve this answer





















          • 6





            Note that this is only available if you have access to the VirtualHost file. It is the recommended method.

            – foochow
            Sep 25 '13 at 23:54






          • 4





            After changing this on httpd.conf, restart apache web server. so that it will reflect and clear your browser cache too.

            – Suriyan Suresh
            Oct 16 '13 at 5:38






          • 1





            This method seems not to work with IE 11. When trying to open domain.com with a redirection to domain.com it is, but a second access to domain.com/folder leads into an error message that IE is not able to open the page. I cannot explain it, but I guess IE caches the redirect and has an issue by resolving the folder through the cache. Firefox works perfectly... I switched to the Rewrite solution by IdemeNaHavaj.

            – Rick-Rainer Ludwig
            Feb 27 '14 at 20:14






          • 1





            I would like to report that this method didn't work for me with Ubuntu 12.4, however the proposed RewriteEngine answer did the trick.

            – Deano
            Jul 25 '14 at 14:37






          • 2





            do you have to do a restart? a reload is much less destructive and will bring in the new config file. /etc/init.d/httpd reload || service httpd reload

            – Dez
            Jan 6 '16 at 15:42



















          107














          RewriteEngine On
          RewriteCond %{HTTPS} off
          RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}


          http://www.sslshopper.com/apache-redirect-http-to-https.html



          or



          http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html






          share|improve this answer





















          • 2





            This is a better solution than the approved one, because it works even if you are behind an SSL offloader like Pound or BigIP. Those offloader will often pass all the traffic onto the same port,and the approved solution won't work in that specific case

            – spiritoo
            Jul 31 '15 at 19:34








          • 1





            @spiritoo Not so. The Apache docs specifically say that this is one of those situations where you should not use mod_rewrite and should rather use Redirect: httpd.apache.org/docs/2.4/rewrite/avoid.html

            – Luke Madhanga
            Nov 23 '15 at 15:07






          • 3





            @LukeMadhanga Apache docrecommands using Redirect for performance. But still, the RewriteEngine solution is better, in the sense of more generic, because it works even in the case I described (offloading). The goal of my comment is to provide every user the key to choose between the two answers. Some people want generic procedures (big corps), others want performance... it's a free choice.

            – spiritoo
            Dec 11 '15 at 9:16






          • 13





            This is great, however, if you want to make it greater then add this [R=302,L,QSA] so any parameters are also passed to the secure page. It should look like: %{REQUEST_URI} [R=302,L,QSA]

            – Svetoslav Marinov
            Jun 11 '16 at 15:19






          • 1





            @SvetoslavMarinov Best answer. Very complete an elegant solution

            – alfredocambera
            Aug 11 '16 at 14:09



















          80














          Searched for apache redirect http to https and landed here. This is what i did on ubuntu:



          1) Enable modules



          sudo a2enmod rewrite
          sudo a2enmod ssl


          2) Edit your site config



          Edit file



          /etc/apache2/sites-available/000-default.conf


          Content should be:



          <VirtualHost *:80>
          RewriteEngine On
          RewriteCond %{HTTPS} off
          RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
          </VirtualHost>

          <VirtualHost *:443>
          SSLEngine on
          SSLCertificateFile <path to your crt file>
          SSLCertificateKeyFile <path to your private key file>

          # Rest of your site config
          # ...
          </VirtualHost>



          • Note that the SSL module requires certificate. you will need to specify existing one (if you bought one) or to generate a self-signed certificate by yourself.


          3) Restart apache2



          sudo service apache2 restart





          share|improve this answer





















          • 6





            SEO: Use 301 redirects to point all HTTP URLs to HTTPS!!!

            – Andrei Krasutski
            Feb 28 '17 at 16:43






          • 1





            Very good annwer

            – Hardik Shah
            Jul 6 '17 at 15:31



















          10














          Actually, your topic is belongs on https://serverfault.com/ but you can still try to check these .htaccess directives:



          RewriteEngine on
          RewriteCond %{HTTPS} off
          RewriteRule ^(.*) https://%{HTTP_HOST}/$1





          share|improve this answer

































            8














            Using mod_rewrite is not the recommended way instead use virtual host and redirect.



            In case, if you are inclined to do using mod_rewrite:



            RewriteEngine On
            # This will enable the Rewrite capabilities

            RewriteCond %{HTTPS} !=on
            # This checks to make sure the connection is not already HTTPS

            RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
            # This rule will redirect users from their original location, to the same
            location but using HTTPS.
            # i.e. http://www.example.com/foo/ to https://www.example.com/foo/
            # The leading slash is made optional so that this will work either in
            # httpd.conf or .htaccess context


            Reference: Httpd Wiki - RewriteHTTPToHTTPS



            If you are looking for a 301 Permanent Redirect, then redirect flag should be as,



             R=301


            so the RewriteRule will be like,



            RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]





            share|improve this answer

































              4














              If you have Apache2.4 check 000-default.conf - remove DocumentRoot and add



              Redirect permanent / https://[your-domain]/





              share|improve this answer

































                3














                This worked for me:



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





                share|improve this answer































                  3














                  Server version: Apache/2.4.29 (Ubuntu)



                  After long search on the web and in the official documentation of apache, the only solution that worked for me came from /usr/share/doc/apache2/README.Debian.gz



                  To enable SSL, type (as user root):

                  a2ensite default-ssl
                  a2enmod ssl


                  In the file /etc/apache2/sites-available/000-default.conf add the




                  Redirect "/" "https://sub.domain.com/"




                  <VirtualHost *:80>

                  #ServerName www.example.com
                  DocumentRoot /var/www/owncloud
                  Redirect "/" "https://sub.domain.com/"


                  That's it.





                  P.S: If you want to read the manual without extracting:



                  gunzip -cd /usr/share/doc/apache2/README.Debian.gz





                  share|improve this answer

































                    2














                    This code work for me.





                    # ----------port 80----------
                    RewriteEngine on
                    # redirect http non-www to https www
                    RewriteCond %{HTTPS} off
                    RewriteCond %{SERVER_NAME} =example.com
                    RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

                    # redirect http www to https www
                    RewriteCond %{HTTPS} off
                    RewriteCond %{SERVER_NAME} =www.example.com
                    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]






                    # ----------port 443----------
                    RewriteEngine on
                    # redirect https non-www to https www
                    RewriteCond %{SERVER_NAME} !^www.(.*)$ [NC]
                    RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]







                    share|improve this answer































                      1














                      Please try this one in apache Virtualhosting configuration
                      and then reload apache service



                      RewriteEngine On

                      RewriteCond %{HTTPS} off


                      RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}





                      share|improve this answer

























                        Your Answer






                        StackExchange.ifUsing("editor", function () {
                        StackExchange.using("externalEditor", function () {
                        StackExchange.using("snippets", function () {
                        StackExchange.snippets.init();
                        });
                        });
                        }, "code-snippets");

                        StackExchange.ready(function() {
                        var channelOptions = {
                        tags: "".split(" "),
                        id: "1"
                        };
                        initTagRenderer("".split(" "), "".split(" "), channelOptions);

                        StackExchange.using("externalEditor", function() {
                        // Have to fire editor after snippets, if snippets enabled
                        if (StackExchange.settings.snippets.snippetsEnabled) {
                        StackExchange.using("snippets", function() {
                        createEditor();
                        });
                        }
                        else {
                        createEditor();
                        }
                        });

                        function createEditor() {
                        StackExchange.prepareEditor({
                        heartbeatType: 'answer',
                        autoActivateHeartbeat: false,
                        convertImagesToLinks: true,
                        noModals: true,
                        showLowRepImageUploadWarning: true,
                        reputationToPostImages: 10,
                        bindNavPrevention: true,
                        postfix: "",
                        imageUploader: {
                        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                        allowUrls: true
                        },
                        onDemand: true,
                        discardSelector: ".discard-answer"
                        ,immediatelyShowMarkdownHelp:true
                        });


                        }
                        });














                        draft saved

                        draft discarded


















                        StackExchange.ready(
                        function () {
                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16200501%2fhttp-to-https-apache-redirection%23new-answer', 'question_page');
                        }
                        );

                        Post as a guest















                        Required, but never shown

























                        10 Answers
                        10






                        active

                        oldest

                        votes








                        10 Answers
                        10






                        active

                        oldest

                        votes









                        active

                        oldest

                        votes






                        active

                        oldest

                        votes









                        189














                        I have actually followed this example and it worked for me :)



                        NameVirtualHost *:80
                        <VirtualHost *:80>
                        ServerName mysite.example.com
                        DocumentRoot /usr/local/apache2/htdocs
                        Redirect permanent / https://mysite.example.com/
                        </VirtualHost>

                        <VirtualHost _default_:443>
                        ServerName mysite.example.com
                        DocumentRoot /usr/local/apache2/htdocs
                        SSLEngine On
                        # etc...
                        </VirtualHost>


                        Then do:



                        /etc/init.d/httpd restart






                        share|improve this answer





















                        • 6





                          Note that this is only available if you have access to the VirtualHost file. It is the recommended method.

                          – foochow
                          Sep 25 '13 at 23:54






                        • 4





                          After changing this on httpd.conf, restart apache web server. so that it will reflect and clear your browser cache too.

                          – Suriyan Suresh
                          Oct 16 '13 at 5:38






                        • 1





                          This method seems not to work with IE 11. When trying to open domain.com with a redirection to domain.com it is, but a second access to domain.com/folder leads into an error message that IE is not able to open the page. I cannot explain it, but I guess IE caches the redirect and has an issue by resolving the folder through the cache. Firefox works perfectly... I switched to the Rewrite solution by IdemeNaHavaj.

                          – Rick-Rainer Ludwig
                          Feb 27 '14 at 20:14






                        • 1





                          I would like to report that this method didn't work for me with Ubuntu 12.4, however the proposed RewriteEngine answer did the trick.

                          – Deano
                          Jul 25 '14 at 14:37






                        • 2





                          do you have to do a restart? a reload is much less destructive and will bring in the new config file. /etc/init.d/httpd reload || service httpd reload

                          – Dez
                          Jan 6 '16 at 15:42
















                        189














                        I have actually followed this example and it worked for me :)



                        NameVirtualHost *:80
                        <VirtualHost *:80>
                        ServerName mysite.example.com
                        DocumentRoot /usr/local/apache2/htdocs
                        Redirect permanent / https://mysite.example.com/
                        </VirtualHost>

                        <VirtualHost _default_:443>
                        ServerName mysite.example.com
                        DocumentRoot /usr/local/apache2/htdocs
                        SSLEngine On
                        # etc...
                        </VirtualHost>


                        Then do:



                        /etc/init.d/httpd restart






                        share|improve this answer





















                        • 6





                          Note that this is only available if you have access to the VirtualHost file. It is the recommended method.

                          – foochow
                          Sep 25 '13 at 23:54






                        • 4





                          After changing this on httpd.conf, restart apache web server. so that it will reflect and clear your browser cache too.

                          – Suriyan Suresh
                          Oct 16 '13 at 5:38






                        • 1





                          This method seems not to work with IE 11. When trying to open domain.com with a redirection to domain.com it is, but a second access to domain.com/folder leads into an error message that IE is not able to open the page. I cannot explain it, but I guess IE caches the redirect and has an issue by resolving the folder through the cache. Firefox works perfectly... I switched to the Rewrite solution by IdemeNaHavaj.

                          – Rick-Rainer Ludwig
                          Feb 27 '14 at 20:14






                        • 1





                          I would like to report that this method didn't work for me with Ubuntu 12.4, however the proposed RewriteEngine answer did the trick.

                          – Deano
                          Jul 25 '14 at 14:37






                        • 2





                          do you have to do a restart? a reload is much less destructive and will bring in the new config file. /etc/init.d/httpd reload || service httpd reload

                          – Dez
                          Jan 6 '16 at 15:42














                        189












                        189








                        189







                        I have actually followed this example and it worked for me :)



                        NameVirtualHost *:80
                        <VirtualHost *:80>
                        ServerName mysite.example.com
                        DocumentRoot /usr/local/apache2/htdocs
                        Redirect permanent / https://mysite.example.com/
                        </VirtualHost>

                        <VirtualHost _default_:443>
                        ServerName mysite.example.com
                        DocumentRoot /usr/local/apache2/htdocs
                        SSLEngine On
                        # etc...
                        </VirtualHost>


                        Then do:



                        /etc/init.d/httpd restart






                        share|improve this answer















                        I have actually followed this example and it worked for me :)



                        NameVirtualHost *:80
                        <VirtualHost *:80>
                        ServerName mysite.example.com
                        DocumentRoot /usr/local/apache2/htdocs
                        Redirect permanent / https://mysite.example.com/
                        </VirtualHost>

                        <VirtualHost _default_:443>
                        ServerName mysite.example.com
                        DocumentRoot /usr/local/apache2/htdocs
                        SSLEngine On
                        # etc...
                        </VirtualHost>


                        Then do:



                        /etc/init.d/httpd restart







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jan 14 '15 at 11:47









                        crmpicco

                        8,5381788158




                        8,5381788158










                        answered Apr 24 '13 at 20:35









                        DeanoDeano

                        4,637103974




                        4,637103974








                        • 6





                          Note that this is only available if you have access to the VirtualHost file. It is the recommended method.

                          – foochow
                          Sep 25 '13 at 23:54






                        • 4





                          After changing this on httpd.conf, restart apache web server. so that it will reflect and clear your browser cache too.

                          – Suriyan Suresh
                          Oct 16 '13 at 5:38






                        • 1





                          This method seems not to work with IE 11. When trying to open domain.com with a redirection to domain.com it is, but a second access to domain.com/folder leads into an error message that IE is not able to open the page. I cannot explain it, but I guess IE caches the redirect and has an issue by resolving the folder through the cache. Firefox works perfectly... I switched to the Rewrite solution by IdemeNaHavaj.

                          – Rick-Rainer Ludwig
                          Feb 27 '14 at 20:14






                        • 1





                          I would like to report that this method didn't work for me with Ubuntu 12.4, however the proposed RewriteEngine answer did the trick.

                          – Deano
                          Jul 25 '14 at 14:37






                        • 2





                          do you have to do a restart? a reload is much less destructive and will bring in the new config file. /etc/init.d/httpd reload || service httpd reload

                          – Dez
                          Jan 6 '16 at 15:42














                        • 6





                          Note that this is only available if you have access to the VirtualHost file. It is the recommended method.

                          – foochow
                          Sep 25 '13 at 23:54






                        • 4





                          After changing this on httpd.conf, restart apache web server. so that it will reflect and clear your browser cache too.

                          – Suriyan Suresh
                          Oct 16 '13 at 5:38






                        • 1





                          This method seems not to work with IE 11. When trying to open domain.com with a redirection to domain.com it is, but a second access to domain.com/folder leads into an error message that IE is not able to open the page. I cannot explain it, but I guess IE caches the redirect and has an issue by resolving the folder through the cache. Firefox works perfectly... I switched to the Rewrite solution by IdemeNaHavaj.

                          – Rick-Rainer Ludwig
                          Feb 27 '14 at 20:14






                        • 1





                          I would like to report that this method didn't work for me with Ubuntu 12.4, however the proposed RewriteEngine answer did the trick.

                          – Deano
                          Jul 25 '14 at 14:37






                        • 2





                          do you have to do a restart? a reload is much less destructive and will bring in the new config file. /etc/init.d/httpd reload || service httpd reload

                          – Dez
                          Jan 6 '16 at 15:42








                        6




                        6





                        Note that this is only available if you have access to the VirtualHost file. It is the recommended method.

                        – foochow
                        Sep 25 '13 at 23:54





                        Note that this is only available if you have access to the VirtualHost file. It is the recommended method.

                        – foochow
                        Sep 25 '13 at 23:54




                        4




                        4





                        After changing this on httpd.conf, restart apache web server. so that it will reflect and clear your browser cache too.

                        – Suriyan Suresh
                        Oct 16 '13 at 5:38





                        After changing this on httpd.conf, restart apache web server. so that it will reflect and clear your browser cache too.

                        – Suriyan Suresh
                        Oct 16 '13 at 5:38




                        1




                        1





                        This method seems not to work with IE 11. When trying to open domain.com with a redirection to domain.com it is, but a second access to domain.com/folder leads into an error message that IE is not able to open the page. I cannot explain it, but I guess IE caches the redirect and has an issue by resolving the folder through the cache. Firefox works perfectly... I switched to the Rewrite solution by IdemeNaHavaj.

                        – Rick-Rainer Ludwig
                        Feb 27 '14 at 20:14





                        This method seems not to work with IE 11. When trying to open domain.com with a redirection to domain.com it is, but a second access to domain.com/folder leads into an error message that IE is not able to open the page. I cannot explain it, but I guess IE caches the redirect and has an issue by resolving the folder through the cache. Firefox works perfectly... I switched to the Rewrite solution by IdemeNaHavaj.

                        – Rick-Rainer Ludwig
                        Feb 27 '14 at 20:14




                        1




                        1





                        I would like to report that this method didn't work for me with Ubuntu 12.4, however the proposed RewriteEngine answer did the trick.

                        – Deano
                        Jul 25 '14 at 14:37





                        I would like to report that this method didn't work for me with Ubuntu 12.4, however the proposed RewriteEngine answer did the trick.

                        – Deano
                        Jul 25 '14 at 14:37




                        2




                        2





                        do you have to do a restart? a reload is much less destructive and will bring in the new config file. /etc/init.d/httpd reload || service httpd reload

                        – Dez
                        Jan 6 '16 at 15:42





                        do you have to do a restart? a reload is much less destructive and will bring in the new config file. /etc/init.d/httpd reload || service httpd reload

                        – Dez
                        Jan 6 '16 at 15:42













                        107














                        RewriteEngine On
                        RewriteCond %{HTTPS} off
                        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}


                        http://www.sslshopper.com/apache-redirect-http-to-https.html



                        or



                        http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html






                        share|improve this answer





















                        • 2





                          This is a better solution than the approved one, because it works even if you are behind an SSL offloader like Pound or BigIP. Those offloader will often pass all the traffic onto the same port,and the approved solution won't work in that specific case

                          – spiritoo
                          Jul 31 '15 at 19:34








                        • 1





                          @spiritoo Not so. The Apache docs specifically say that this is one of those situations where you should not use mod_rewrite and should rather use Redirect: httpd.apache.org/docs/2.4/rewrite/avoid.html

                          – Luke Madhanga
                          Nov 23 '15 at 15:07






                        • 3





                          @LukeMadhanga Apache docrecommands using Redirect for performance. But still, the RewriteEngine solution is better, in the sense of more generic, because it works even in the case I described (offloading). The goal of my comment is to provide every user the key to choose between the two answers. Some people want generic procedures (big corps), others want performance... it's a free choice.

                          – spiritoo
                          Dec 11 '15 at 9:16






                        • 13





                          This is great, however, if you want to make it greater then add this [R=302,L,QSA] so any parameters are also passed to the secure page. It should look like: %{REQUEST_URI} [R=302,L,QSA]

                          – Svetoslav Marinov
                          Jun 11 '16 at 15:19






                        • 1





                          @SvetoslavMarinov Best answer. Very complete an elegant solution

                          – alfredocambera
                          Aug 11 '16 at 14:09
















                        107














                        RewriteEngine On
                        RewriteCond %{HTTPS} off
                        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}


                        http://www.sslshopper.com/apache-redirect-http-to-https.html



                        or



                        http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html






                        share|improve this answer





















                        • 2





                          This is a better solution than the approved one, because it works even if you are behind an SSL offloader like Pound or BigIP. Those offloader will often pass all the traffic onto the same port,and the approved solution won't work in that specific case

                          – spiritoo
                          Jul 31 '15 at 19:34








                        • 1





                          @spiritoo Not so. The Apache docs specifically say that this is one of those situations where you should not use mod_rewrite and should rather use Redirect: httpd.apache.org/docs/2.4/rewrite/avoid.html

                          – Luke Madhanga
                          Nov 23 '15 at 15:07






                        • 3





                          @LukeMadhanga Apache docrecommands using Redirect for performance. But still, the RewriteEngine solution is better, in the sense of more generic, because it works even in the case I described (offloading). The goal of my comment is to provide every user the key to choose between the two answers. Some people want generic procedures (big corps), others want performance... it's a free choice.

                          – spiritoo
                          Dec 11 '15 at 9:16






                        • 13





                          This is great, however, if you want to make it greater then add this [R=302,L,QSA] so any parameters are also passed to the secure page. It should look like: %{REQUEST_URI} [R=302,L,QSA]

                          – Svetoslav Marinov
                          Jun 11 '16 at 15:19






                        • 1





                          @SvetoslavMarinov Best answer. Very complete an elegant solution

                          – alfredocambera
                          Aug 11 '16 at 14:09














                        107












                        107








                        107







                        RewriteEngine On
                        RewriteCond %{HTTPS} off
                        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}


                        http://www.sslshopper.com/apache-redirect-http-to-https.html



                        or



                        http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html






                        share|improve this answer















                        RewriteEngine On
                        RewriteCond %{HTTPS} off
                        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}


                        http://www.sslshopper.com/apache-redirect-http-to-https.html



                        or



                        http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited May 18 '14 at 11:32









                        AD7six

                        47.5k1074108




                        47.5k1074108










                        answered Apr 24 '13 at 19:27









                        IdemeNaHavajIdemeNaHavaj

                        1,224179




                        1,224179








                        • 2





                          This is a better solution than the approved one, because it works even if you are behind an SSL offloader like Pound or BigIP. Those offloader will often pass all the traffic onto the same port,and the approved solution won't work in that specific case

                          – spiritoo
                          Jul 31 '15 at 19:34








                        • 1





                          @spiritoo Not so. The Apache docs specifically say that this is one of those situations where you should not use mod_rewrite and should rather use Redirect: httpd.apache.org/docs/2.4/rewrite/avoid.html

                          – Luke Madhanga
                          Nov 23 '15 at 15:07






                        • 3





                          @LukeMadhanga Apache docrecommands using Redirect for performance. But still, the RewriteEngine solution is better, in the sense of more generic, because it works even in the case I described (offloading). The goal of my comment is to provide every user the key to choose between the two answers. Some people want generic procedures (big corps), others want performance... it's a free choice.

                          – spiritoo
                          Dec 11 '15 at 9:16






                        • 13





                          This is great, however, if you want to make it greater then add this [R=302,L,QSA] so any parameters are also passed to the secure page. It should look like: %{REQUEST_URI} [R=302,L,QSA]

                          – Svetoslav Marinov
                          Jun 11 '16 at 15:19






                        • 1





                          @SvetoslavMarinov Best answer. Very complete an elegant solution

                          – alfredocambera
                          Aug 11 '16 at 14:09














                        • 2





                          This is a better solution than the approved one, because it works even if you are behind an SSL offloader like Pound or BigIP. Those offloader will often pass all the traffic onto the same port,and the approved solution won't work in that specific case

                          – spiritoo
                          Jul 31 '15 at 19:34








                        • 1





                          @spiritoo Not so. The Apache docs specifically say that this is one of those situations where you should not use mod_rewrite and should rather use Redirect: httpd.apache.org/docs/2.4/rewrite/avoid.html

                          – Luke Madhanga
                          Nov 23 '15 at 15:07






                        • 3





                          @LukeMadhanga Apache docrecommands using Redirect for performance. But still, the RewriteEngine solution is better, in the sense of more generic, because it works even in the case I described (offloading). The goal of my comment is to provide every user the key to choose between the two answers. Some people want generic procedures (big corps), others want performance... it's a free choice.

                          – spiritoo
                          Dec 11 '15 at 9:16






                        • 13





                          This is great, however, if you want to make it greater then add this [R=302,L,QSA] so any parameters are also passed to the secure page. It should look like: %{REQUEST_URI} [R=302,L,QSA]

                          – Svetoslav Marinov
                          Jun 11 '16 at 15:19






                        • 1





                          @SvetoslavMarinov Best answer. Very complete an elegant solution

                          – alfredocambera
                          Aug 11 '16 at 14:09








                        2




                        2





                        This is a better solution than the approved one, because it works even if you are behind an SSL offloader like Pound or BigIP. Those offloader will often pass all the traffic onto the same port,and the approved solution won't work in that specific case

                        – spiritoo
                        Jul 31 '15 at 19:34







                        This is a better solution than the approved one, because it works even if you are behind an SSL offloader like Pound or BigIP. Those offloader will often pass all the traffic onto the same port,and the approved solution won't work in that specific case

                        – spiritoo
                        Jul 31 '15 at 19:34






                        1




                        1





                        @spiritoo Not so. The Apache docs specifically say that this is one of those situations where you should not use mod_rewrite and should rather use Redirect: httpd.apache.org/docs/2.4/rewrite/avoid.html

                        – Luke Madhanga
                        Nov 23 '15 at 15:07





                        @spiritoo Not so. The Apache docs specifically say that this is one of those situations where you should not use mod_rewrite and should rather use Redirect: httpd.apache.org/docs/2.4/rewrite/avoid.html

                        – Luke Madhanga
                        Nov 23 '15 at 15:07




                        3




                        3





                        @LukeMadhanga Apache docrecommands using Redirect for performance. But still, the RewriteEngine solution is better, in the sense of more generic, because it works even in the case I described (offloading). The goal of my comment is to provide every user the key to choose between the two answers. Some people want generic procedures (big corps), others want performance... it's a free choice.

                        – spiritoo
                        Dec 11 '15 at 9:16





                        @LukeMadhanga Apache docrecommands using Redirect for performance. But still, the RewriteEngine solution is better, in the sense of more generic, because it works even in the case I described (offloading). The goal of my comment is to provide every user the key to choose between the two answers. Some people want generic procedures (big corps), others want performance... it's a free choice.

                        – spiritoo
                        Dec 11 '15 at 9:16




                        13




                        13





                        This is great, however, if you want to make it greater then add this [R=302,L,QSA] so any parameters are also passed to the secure page. It should look like: %{REQUEST_URI} [R=302,L,QSA]

                        – Svetoslav Marinov
                        Jun 11 '16 at 15:19





                        This is great, however, if you want to make it greater then add this [R=302,L,QSA] so any parameters are also passed to the secure page. It should look like: %{REQUEST_URI} [R=302,L,QSA]

                        – Svetoslav Marinov
                        Jun 11 '16 at 15:19




                        1




                        1





                        @SvetoslavMarinov Best answer. Very complete an elegant solution

                        – alfredocambera
                        Aug 11 '16 at 14:09





                        @SvetoslavMarinov Best answer. Very complete an elegant solution

                        – alfredocambera
                        Aug 11 '16 at 14:09











                        80














                        Searched for apache redirect http to https and landed here. This is what i did on ubuntu:



                        1) Enable modules



                        sudo a2enmod rewrite
                        sudo a2enmod ssl


                        2) Edit your site config



                        Edit file



                        /etc/apache2/sites-available/000-default.conf


                        Content should be:



                        <VirtualHost *:80>
                        RewriteEngine On
                        RewriteCond %{HTTPS} off
                        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
                        </VirtualHost>

                        <VirtualHost *:443>
                        SSLEngine on
                        SSLCertificateFile <path to your crt file>
                        SSLCertificateKeyFile <path to your private key file>

                        # Rest of your site config
                        # ...
                        </VirtualHost>



                        • Note that the SSL module requires certificate. you will need to specify existing one (if you bought one) or to generate a self-signed certificate by yourself.


                        3) Restart apache2



                        sudo service apache2 restart





                        share|improve this answer





















                        • 6





                          SEO: Use 301 redirects to point all HTTP URLs to HTTPS!!!

                          – Andrei Krasutski
                          Feb 28 '17 at 16:43






                        • 1





                          Very good annwer

                          – Hardik Shah
                          Jul 6 '17 at 15:31
















                        80














                        Searched for apache redirect http to https and landed here. This is what i did on ubuntu:



                        1) Enable modules



                        sudo a2enmod rewrite
                        sudo a2enmod ssl


                        2) Edit your site config



                        Edit file



                        /etc/apache2/sites-available/000-default.conf


                        Content should be:



                        <VirtualHost *:80>
                        RewriteEngine On
                        RewriteCond %{HTTPS} off
                        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
                        </VirtualHost>

                        <VirtualHost *:443>
                        SSLEngine on
                        SSLCertificateFile <path to your crt file>
                        SSLCertificateKeyFile <path to your private key file>

                        # Rest of your site config
                        # ...
                        </VirtualHost>



                        • Note that the SSL module requires certificate. you will need to specify existing one (if you bought one) or to generate a self-signed certificate by yourself.


                        3) Restart apache2



                        sudo service apache2 restart





                        share|improve this answer





















                        • 6





                          SEO: Use 301 redirects to point all HTTP URLs to HTTPS!!!

                          – Andrei Krasutski
                          Feb 28 '17 at 16:43






                        • 1





                          Very good annwer

                          – Hardik Shah
                          Jul 6 '17 at 15:31














                        80












                        80








                        80







                        Searched for apache redirect http to https and landed here. This is what i did on ubuntu:



                        1) Enable modules



                        sudo a2enmod rewrite
                        sudo a2enmod ssl


                        2) Edit your site config



                        Edit file



                        /etc/apache2/sites-available/000-default.conf


                        Content should be:



                        <VirtualHost *:80>
                        RewriteEngine On
                        RewriteCond %{HTTPS} off
                        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
                        </VirtualHost>

                        <VirtualHost *:443>
                        SSLEngine on
                        SSLCertificateFile <path to your crt file>
                        SSLCertificateKeyFile <path to your private key file>

                        # Rest of your site config
                        # ...
                        </VirtualHost>



                        • Note that the SSL module requires certificate. you will need to specify existing one (if you bought one) or to generate a self-signed certificate by yourself.


                        3) Restart apache2



                        sudo service apache2 restart





                        share|improve this answer















                        Searched for apache redirect http to https and landed here. This is what i did on ubuntu:



                        1) Enable modules



                        sudo a2enmod rewrite
                        sudo a2enmod ssl


                        2) Edit your site config



                        Edit file



                        /etc/apache2/sites-available/000-default.conf


                        Content should be:



                        <VirtualHost *:80>
                        RewriteEngine On
                        RewriteCond %{HTTPS} off
                        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
                        </VirtualHost>

                        <VirtualHost *:443>
                        SSLEngine on
                        SSLCertificateFile <path to your crt file>
                        SSLCertificateKeyFile <path to your private key file>

                        # Rest of your site config
                        # ...
                        </VirtualHost>



                        • Note that the SSL module requires certificate. you will need to specify existing one (if you bought one) or to generate a self-signed certificate by yourself.


                        3) Restart apache2



                        sudo service apache2 restart






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited May 31 '16 at 11:27

























                        answered Nov 3 '14 at 15:49









                        Jossef HarushJossef Harush

                        16.3k46675




                        16.3k46675








                        • 6





                          SEO: Use 301 redirects to point all HTTP URLs to HTTPS!!!

                          – Andrei Krasutski
                          Feb 28 '17 at 16:43






                        • 1





                          Very good annwer

                          – Hardik Shah
                          Jul 6 '17 at 15:31














                        • 6





                          SEO: Use 301 redirects to point all HTTP URLs to HTTPS!!!

                          – Andrei Krasutski
                          Feb 28 '17 at 16:43






                        • 1





                          Very good annwer

                          – Hardik Shah
                          Jul 6 '17 at 15:31








                        6




                        6





                        SEO: Use 301 redirects to point all HTTP URLs to HTTPS!!!

                        – Andrei Krasutski
                        Feb 28 '17 at 16:43





                        SEO: Use 301 redirects to point all HTTP URLs to HTTPS!!!

                        – Andrei Krasutski
                        Feb 28 '17 at 16:43




                        1




                        1





                        Very good annwer

                        – Hardik Shah
                        Jul 6 '17 at 15:31





                        Very good annwer

                        – Hardik Shah
                        Jul 6 '17 at 15:31











                        10














                        Actually, your topic is belongs on https://serverfault.com/ but you can still try to check these .htaccess directives:



                        RewriteEngine on
                        RewriteCond %{HTTPS} off
                        RewriteRule ^(.*) https://%{HTTP_HOST}/$1





                        share|improve this answer






























                          10














                          Actually, your topic is belongs on https://serverfault.com/ but you can still try to check these .htaccess directives:



                          RewriteEngine on
                          RewriteCond %{HTTPS} off
                          RewriteRule ^(.*) https://%{HTTP_HOST}/$1





                          share|improve this answer




























                            10












                            10








                            10







                            Actually, your topic is belongs on https://serverfault.com/ but you can still try to check these .htaccess directives:



                            RewriteEngine on
                            RewriteCond %{HTTPS} off
                            RewriteRule ^(.*) https://%{HTTP_HOST}/$1





                            share|improve this answer















                            Actually, your topic is belongs on https://serverfault.com/ but you can still try to check these .htaccess directives:



                            RewriteEngine on
                            RewriteCond %{HTTPS} off
                            RewriteRule ^(.*) https://%{HTTP_HOST}/$1






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Apr 13 '17 at 12:13









                            Community

                            11




                            11










                            answered Apr 24 '13 at 19:34









                            5ervant5ervant

                            2,45952350




                            2,45952350























                                8














                                Using mod_rewrite is not the recommended way instead use virtual host and redirect.



                                In case, if you are inclined to do using mod_rewrite:



                                RewriteEngine On
                                # This will enable the Rewrite capabilities

                                RewriteCond %{HTTPS} !=on
                                # This checks to make sure the connection is not already HTTPS

                                RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
                                # This rule will redirect users from their original location, to the same
                                location but using HTTPS.
                                # i.e. http://www.example.com/foo/ to https://www.example.com/foo/
                                # The leading slash is made optional so that this will work either in
                                # httpd.conf or .htaccess context


                                Reference: Httpd Wiki - RewriteHTTPToHTTPS



                                If you are looking for a 301 Permanent Redirect, then redirect flag should be as,



                                 R=301


                                so the RewriteRule will be like,



                                RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]





                                share|improve this answer






























                                  8














                                  Using mod_rewrite is not the recommended way instead use virtual host and redirect.



                                  In case, if you are inclined to do using mod_rewrite:



                                  RewriteEngine On
                                  # This will enable the Rewrite capabilities

                                  RewriteCond %{HTTPS} !=on
                                  # This checks to make sure the connection is not already HTTPS

                                  RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
                                  # This rule will redirect users from their original location, to the same
                                  location but using HTTPS.
                                  # i.e. http://www.example.com/foo/ to https://www.example.com/foo/
                                  # The leading slash is made optional so that this will work either in
                                  # httpd.conf or .htaccess context


                                  Reference: Httpd Wiki - RewriteHTTPToHTTPS



                                  If you are looking for a 301 Permanent Redirect, then redirect flag should be as,



                                   R=301


                                  so the RewriteRule will be like,



                                  RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]





                                  share|improve this answer




























                                    8












                                    8








                                    8







                                    Using mod_rewrite is not the recommended way instead use virtual host and redirect.



                                    In case, if you are inclined to do using mod_rewrite:



                                    RewriteEngine On
                                    # This will enable the Rewrite capabilities

                                    RewriteCond %{HTTPS} !=on
                                    # This checks to make sure the connection is not already HTTPS

                                    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
                                    # This rule will redirect users from their original location, to the same
                                    location but using HTTPS.
                                    # i.e. http://www.example.com/foo/ to https://www.example.com/foo/
                                    # The leading slash is made optional so that this will work either in
                                    # httpd.conf or .htaccess context


                                    Reference: Httpd Wiki - RewriteHTTPToHTTPS



                                    If you are looking for a 301 Permanent Redirect, then redirect flag should be as,



                                     R=301


                                    so the RewriteRule will be like,



                                    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]





                                    share|improve this answer















                                    Using mod_rewrite is not the recommended way instead use virtual host and redirect.



                                    In case, if you are inclined to do using mod_rewrite:



                                    RewriteEngine On
                                    # This will enable the Rewrite capabilities

                                    RewriteCond %{HTTPS} !=on
                                    # This checks to make sure the connection is not already HTTPS

                                    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
                                    # This rule will redirect users from their original location, to the same
                                    location but using HTTPS.
                                    # i.e. http://www.example.com/foo/ to https://www.example.com/foo/
                                    # The leading slash is made optional so that this will work either in
                                    # httpd.conf or .htaccess context


                                    Reference: Httpd Wiki - RewriteHTTPToHTTPS



                                    If you are looking for a 301 Permanent Redirect, then redirect flag should be as,



                                     R=301


                                    so the RewriteRule will be like,



                                    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 15 '17 at 8:16

























                                    answered Nov 15 '17 at 7:59









                                    VincyVincy

                                    8614




                                    8614























                                        4














                                        If you have Apache2.4 check 000-default.conf - remove DocumentRoot and add



                                        Redirect permanent / https://[your-domain]/





                                        share|improve this answer






























                                          4














                                          If you have Apache2.4 check 000-default.conf - remove DocumentRoot and add



                                          Redirect permanent / https://[your-domain]/





                                          share|improve this answer




























                                            4












                                            4








                                            4







                                            If you have Apache2.4 check 000-default.conf - remove DocumentRoot and add



                                            Redirect permanent / https://[your-domain]/





                                            share|improve this answer















                                            If you have Apache2.4 check 000-default.conf - remove DocumentRoot and add



                                            Redirect permanent / https://[your-domain]/






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Jan 27 '18 at 23:13









                                            tinlyx

                                            11k2059113




                                            11k2059113










                                            answered Jan 27 '18 at 22:54









                                            indifferenceindifference

                                            4113




                                            4113























                                                3














                                                This worked for me:



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





                                                share|improve this answer




























                                                  3














                                                  This worked for me:



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





                                                  share|improve this answer


























                                                    3












                                                    3








                                                    3







                                                    This worked for me:



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





                                                    share|improve this answer













                                                    This worked for me:



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






                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Apr 5 '16 at 7:41









                                                    FintFint

                                                    57755




                                                    57755























                                                        3














                                                        Server version: Apache/2.4.29 (Ubuntu)



                                                        After long search on the web and in the official documentation of apache, the only solution that worked for me came from /usr/share/doc/apache2/README.Debian.gz



                                                        To enable SSL, type (as user root):

                                                        a2ensite default-ssl
                                                        a2enmod ssl


                                                        In the file /etc/apache2/sites-available/000-default.conf add the




                                                        Redirect "/" "https://sub.domain.com/"




                                                        <VirtualHost *:80>

                                                        #ServerName www.example.com
                                                        DocumentRoot /var/www/owncloud
                                                        Redirect "/" "https://sub.domain.com/"


                                                        That's it.





                                                        P.S: If you want to read the manual without extracting:



                                                        gunzip -cd /usr/share/doc/apache2/README.Debian.gz





                                                        share|improve this answer






























                                                          3














                                                          Server version: Apache/2.4.29 (Ubuntu)



                                                          After long search on the web and in the official documentation of apache, the only solution that worked for me came from /usr/share/doc/apache2/README.Debian.gz



                                                          To enable SSL, type (as user root):

                                                          a2ensite default-ssl
                                                          a2enmod ssl


                                                          In the file /etc/apache2/sites-available/000-default.conf add the




                                                          Redirect "/" "https://sub.domain.com/"




                                                          <VirtualHost *:80>

                                                          #ServerName www.example.com
                                                          DocumentRoot /var/www/owncloud
                                                          Redirect "/" "https://sub.domain.com/"


                                                          That's it.





                                                          P.S: If you want to read the manual without extracting:



                                                          gunzip -cd /usr/share/doc/apache2/README.Debian.gz





                                                          share|improve this answer




























                                                            3












                                                            3








                                                            3







                                                            Server version: Apache/2.4.29 (Ubuntu)



                                                            After long search on the web and in the official documentation of apache, the only solution that worked for me came from /usr/share/doc/apache2/README.Debian.gz



                                                            To enable SSL, type (as user root):

                                                            a2ensite default-ssl
                                                            a2enmod ssl


                                                            In the file /etc/apache2/sites-available/000-default.conf add the




                                                            Redirect "/" "https://sub.domain.com/"




                                                            <VirtualHost *:80>

                                                            #ServerName www.example.com
                                                            DocumentRoot /var/www/owncloud
                                                            Redirect "/" "https://sub.domain.com/"


                                                            That's it.





                                                            P.S: If you want to read the manual without extracting:



                                                            gunzip -cd /usr/share/doc/apache2/README.Debian.gz





                                                            share|improve this answer















                                                            Server version: Apache/2.4.29 (Ubuntu)



                                                            After long search on the web and in the official documentation of apache, the only solution that worked for me came from /usr/share/doc/apache2/README.Debian.gz



                                                            To enable SSL, type (as user root):

                                                            a2ensite default-ssl
                                                            a2enmod ssl


                                                            In the file /etc/apache2/sites-available/000-default.conf add the




                                                            Redirect "/" "https://sub.domain.com/"




                                                            <VirtualHost *:80>

                                                            #ServerName www.example.com
                                                            DocumentRoot /var/www/owncloud
                                                            Redirect "/" "https://sub.domain.com/"


                                                            That's it.





                                                            P.S: If you want to read the manual without extracting:



                                                            gunzip -cd /usr/share/doc/apache2/README.Debian.gz






                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Nov 3 '18 at 12:29

























                                                            answered Nov 1 '18 at 15:14









                                                            DimiDakDimiDak

                                                            710614




                                                            710614























                                                                2














                                                                This code work for me.





                                                                # ----------port 80----------
                                                                RewriteEngine on
                                                                # redirect http non-www to https www
                                                                RewriteCond %{HTTPS} off
                                                                RewriteCond %{SERVER_NAME} =example.com
                                                                RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

                                                                # redirect http www to https www
                                                                RewriteCond %{HTTPS} off
                                                                RewriteCond %{SERVER_NAME} =www.example.com
                                                                RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]






                                                                # ----------port 443----------
                                                                RewriteEngine on
                                                                # redirect https non-www to https www
                                                                RewriteCond %{SERVER_NAME} !^www.(.*)$ [NC]
                                                                RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]







                                                                share|improve this answer




























                                                                  2














                                                                  This code work for me.





                                                                  # ----------port 80----------
                                                                  RewriteEngine on
                                                                  # redirect http non-www to https www
                                                                  RewriteCond %{HTTPS} off
                                                                  RewriteCond %{SERVER_NAME} =example.com
                                                                  RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

                                                                  # redirect http www to https www
                                                                  RewriteCond %{HTTPS} off
                                                                  RewriteCond %{SERVER_NAME} =www.example.com
                                                                  RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]






                                                                  # ----------port 443----------
                                                                  RewriteEngine on
                                                                  # redirect https non-www to https www
                                                                  RewriteCond %{SERVER_NAME} !^www.(.*)$ [NC]
                                                                  RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]







                                                                  share|improve this answer


























                                                                    2












                                                                    2








                                                                    2







                                                                    This code work for me.





                                                                    # ----------port 80----------
                                                                    RewriteEngine on
                                                                    # redirect http non-www to https www
                                                                    RewriteCond %{HTTPS} off
                                                                    RewriteCond %{SERVER_NAME} =example.com
                                                                    RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

                                                                    # redirect http www to https www
                                                                    RewriteCond %{HTTPS} off
                                                                    RewriteCond %{SERVER_NAME} =www.example.com
                                                                    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]






                                                                    # ----------port 443----------
                                                                    RewriteEngine on
                                                                    # redirect https non-www to https www
                                                                    RewriteCond %{SERVER_NAME} !^www.(.*)$ [NC]
                                                                    RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]







                                                                    share|improve this answer













                                                                    This code work for me.





                                                                    # ----------port 80----------
                                                                    RewriteEngine on
                                                                    # redirect http non-www to https www
                                                                    RewriteCond %{HTTPS} off
                                                                    RewriteCond %{SERVER_NAME} =example.com
                                                                    RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

                                                                    # redirect http www to https www
                                                                    RewriteCond %{HTTPS} off
                                                                    RewriteCond %{SERVER_NAME} =www.example.com
                                                                    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]






                                                                    # ----------port 443----------
                                                                    RewriteEngine on
                                                                    # redirect https non-www to https www
                                                                    RewriteCond %{SERVER_NAME} !^www.(.*)$ [NC]
                                                                    RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]








                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Apr 5 '17 at 1:53









                                                                    user7817632user7817632

                                                                    211




                                                                    211























                                                                        1














                                                                        Please try this one in apache Virtualhosting configuration
                                                                        and then reload apache service



                                                                        RewriteEngine On

                                                                        RewriteCond %{HTTPS} off


                                                                        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}





                                                                        share|improve this answer






























                                                                          1














                                                                          Please try this one in apache Virtualhosting configuration
                                                                          and then reload apache service



                                                                          RewriteEngine On

                                                                          RewriteCond %{HTTPS} off


                                                                          RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}





                                                                          share|improve this answer




























                                                                            1












                                                                            1








                                                                            1







                                                                            Please try this one in apache Virtualhosting configuration
                                                                            and then reload apache service



                                                                            RewriteEngine On

                                                                            RewriteCond %{HTTPS} off


                                                                            RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}





                                                                            share|improve this answer















                                                                            Please try this one in apache Virtualhosting configuration
                                                                            and then reload apache service



                                                                            RewriteEngine On

                                                                            RewriteCond %{HTTPS} off


                                                                            RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}






                                                                            share|improve this answer














                                                                            share|improve this answer



                                                                            share|improve this answer








                                                                            edited Jan 3 at 10:11

























                                                                            answered Jan 3 at 7:39









                                                                            MD IRFANMD IRFAN

                                                                            212




                                                                            212






























                                                                                draft saved

                                                                                draft discarded




















































                                                                                Thanks for contributing an answer to Stack Overflow!


                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                But avoid



                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                • Making statements based on opinion; back them up with references or personal experience.


                                                                                To learn more, see our tips on writing great answers.




                                                                                draft saved


                                                                                draft discarded














                                                                                StackExchange.ready(
                                                                                function () {
                                                                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16200501%2fhttp-to-https-apache-redirection%23new-answer', 'question_page');
                                                                                }
                                                                                );

                                                                                Post as a guest















                                                                                Required, but never shown





















































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown

































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown







                                                                                Popular posts from this blog

                                                                                Monofisismo

                                                                                Angular Downloading a file using contenturl with Basic Authentication

                                                                                Olmecas