How to redirect all HTTP requests to HTTPS

前端 未结 26 2180
小鲜肉
小鲜肉 2020-11-22 00:40

I\'m trying to redirect all insecure HTTP requests on my site (e.g. http://www.example.com) to HTTPS (https://www.example.com). I\'m using PHP btw.

相关标签:
26条回答
  • 2020-11-22 01:10

    This redirects all the URLs to https and www

    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTPS_HOST} !^www.example.com$ [NC,OR]
    RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
    
    0 讨论(0)
  • 2020-11-22 01:10

    If you're using an Amazon Web Services Elastic Load Balancer which accepts https traffic and routes it to your server(s) with http, the correct way to redirect all http traffic to https is described here: https://aws.amazon.com/premiumsupport/knowledge-center/redirect-http-https-elb

    Use the X-Forwarded-Proto header (contains http or https) which is always included in http requests from the load balancer, as described here: https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html

    In the httpd.conf file:

    <VirtualHost *:80>
    
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
    
    </VirtualHost>
    

    Or in your root .htaccess file:

    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
    

    Bonus: it will not try to redirect http traffic on your local development machine.

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