htaccess redirect to https://www

前端 未结 14 2036
夕颜
夕颜 2020-11-21 11:01

I have the following htaccess code:



RewriteEngine On
RewriteCond !{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{RE         


        
相关标签:
14条回答
  • 2020-11-21 11:37

    Michals answer worked for me, albeit with one small modification:

    Problem:

    when you have a single site security certificate, a browser that tries to access your page without https:// www. (or whichever domain your certificate covers) will display an ugly red warning screen before it even gets to receive the redirect to the safe and correct https page.

    Solution

    First use the redirect to the www (or whichever domain is covered by your certificate) and only then do the https redirect. This will ensure that your users are not confronted with any error because your browser sees a certificate that doesn't cover the current url.

    #First rewrite any request to the wrong domain to use the correct one (here www.)
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    #Now, rewrite to HTTPS:
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    0 讨论(0)
  • 2020-11-21 11:39

    There are a lot of solutions out there. Here is a link to the apache wiki which deals with this issue directly.

    http://wiki.apache.org/httpd/RewriteHTTPToHTTPS

    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
    
    0 讨论(0)
  • 2020-11-21 11:40
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
    

    Notes: Make sure you have done the following steps

    1. sudo a2enmod rewrite
    2. sudo service apache2 restart
    3. Add Following in your vhost file, located at /etc/apache2/sites-available/000-default.conf
    <Directory /var/www/html>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
      Require all granted
    </Directory>
    

    Now your .htaccess will work and your site will redirect to http:// to https://www

    0 讨论(0)
  • 2020-11-21 11:44
    RewriteEngine On 
    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
    
    0 讨论(0)
  • 2020-11-21 11:47

    I used the below code from this website, it works great https://www.freecodecamp.org/news/how-to-redirect-http-to-https-using-htaccess/

    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

    Hope it helps

    0 讨论(0)
  • 2020-11-21 11:47

    I try first answer and it doesnt work... This work:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{ENV:HTTPS} !=on
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    
    0 讨论(0)
提交回复
热议问题