Correctly switching between HTTP and HTTPS using .htaccess

后端 未结 6 1770
挽巷
挽巷 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 08:40

    I think it should be:

    RewriteCond %{HTTPS}  =on
    ^/checkout(.*) http://shoppingsite.com/checkout$1 [R]
    

    See the mod_rewrite documentation.

    0 讨论(0)
  • 2020-11-28 08:45

    As detailed in this answer, fix your application to use https:// links when needed. Don't rely on automatic redirections, this could lead you to a false sense of security if you haven't made your links/forms served over https:// go to https:// URLs too. Using mod_rewrite automatically makes it harder to detect such mistakes (which can also be vulnerabilities).

    0 讨论(0)
  • 2020-11-28 08:56

    This should work in pretty much every scenario and should work in your actual vhost or .htaccess:

    RewriteEngine on
    RewriteCond %{SERVER_PORT} ^80$
    RewriteRule ^(.*)$ https://%{SERVER_NAME}/%{REQUEST_URI} [R=301,L]
    

    (do not forget the slash before %{REQUEST_URI} as this may allow passing a portnumber, which is dangerous)

    0 讨论(0)
  • 2020-11-28 08:59
    RewriteEngine on
    RewriteCond %{HTTPS} off [OR] 
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{SERVER_NAME}/%{REQUEST_URI} [R=301,L]
    

    I had some problem being behind a loadballancer. This how i fixed it.

    0 讨论(0)
  • 2020-11-28 09:00

    I use something similar to this for my admin folder in wordpress:

    #redirect all https traffic to http, unless it is pointed at /checkout
    RewriteCond %{HTTPS} on
    RewriteCond %{REQUEST_URI} !^/checkout/?.*$
    RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]
    

    The RewriteCond %{HTTPS} on portion may not work for all web servers. My webhost requires RewriteCond %{HTTP:X-Forwarded-SSL} on, for instance.

    If you want to force the reverse, try:

    #redirect all http traffic to https, if it is pointed at /checkout
    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} ^/checkout/?.*$
    RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
    

    If you want some alternate ways to do it, check out askapache.

    0 讨论(0)
  • 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
    <IfModule mod_rewrite.c>
    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]
    </IfModule>
    # 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.

    0 讨论(0)
提交回复
热议问题