Correctly switching between HTTP and HTTPS using .htaccess

后端 未结 6 1780
挽巷
挽巷 2020-11-28 08:14

We\'ve got a shopping site which we\'re hosting on a shared host (Mediatemple Gridserver). Some parts of the site need to use HTTPS (checkout etc) but the rest should be usi

6条回答
  •  [愿得一人]
    2020-11-28 09:02

    For me worked this (I used it for wordpress site and redirecting to HTTPS). You have to add the condition and rule lines just behind RewriteEngine and RewriteBase lines:

    # BEGIN WordPress
    
    RewriteEngine On
    RewriteBase /
    
    # I added these two lines for redirect to HTTPS
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R=301,L]
    # (end of custom modifications)
    
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    # END WordPress`
    

    Have a look to condition RewriteCond %{HTTP:X-Forwarded-Proto} !https - only this worked for my server hosting. (I tried RewriteCond %{SERVER_PORT} !^443$ or RewriteCond %{HTTPS} off as well, but without success.

提交回复
热议问题