How to redirect all HTTP requests to HTTPS

前端 未结 26 2001
小鲜肉
小鲜肉 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 00:44

    This is the html redirect approach it works but not the best.

     <meta http-equiv="Refresh" content="0;URL=https://www.example.com" />
    

    PHP approach

    <?php
    function redirectTohttps() {
        if ($_SERVER['HTTPS']!="on") {
            $redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            header("Location:$redirect"); 
        } 
    }
    ?>
    

    .htaccess approch

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

    copied from: www.letuslook.org

    0 讨论(0)
  • 2020-11-22 00:45

    The Apache docs recommend against using a rewrite:

    To redirect http URLs to https, do the following:

    <VirtualHost *:80>
        ServerName www.example.com
        Redirect / https://www.example.com/
    </VirtualHost>
    
    <VirtualHost *:443>
        ServerName www.example.com
        # ... SSL configuration goes here
    </VirtualHost>
    

    This snippet should go into main server configuration file, not into .htaccess as asked in the question.

    This article might have come up only after the question was asked and answered, but seems to be the current way to go.

    0 讨论(0)
  • 2020-11-22 00:45

    As I was saying in this question, I'd suggest you avoid redirecting all HTTP requests to their HTTPS equivalent blindly, as it may cause you a false impression of security. Instead, you should probably redirect the "root" of your HTTP site to the root of your HTTPS site and link from there, only to HTTPS.

    The problem is that if some link or form on the HTTPS site makes the client send a request to the HTTP site, its content will be visible, before the redirection.

    For example, if one of your pages served over HTTPS has a form that says <form action="http://example.com/doSomething"> and sends some data that shouldn't be sent in clear, the browser will first send the full request (including entity, if it's a POST) to the HTTP site first. The redirection will be sent immediately to the browser and, since a large number of users disable or ignore the warnings, it's likely to be ignored.

    Of course, the mistake of providing the links that should be to the HTTPS site but that end up being for the HTTP site may cause problems as soon as you get something listening on the HTTP port on the same IP address as your HTTPS site. However, I think keeping the two sites as a "mirror" only increases the chances of making mistakes, as you may tend to make the assumption that it will auto-correct itself by redirecting the user to HTTPS, whereas it's often too late. (There were similar discussions in this question.)

    0 讨论(0)
  • 2020-11-22 00:45

    Unless you need mod_rewrite for other things, using Apache core IF directive is cleaner & faster:

    <If "%{HTTPS} == 'off'">
    Redirect permanent / https://yoursite.com/
    </If>
    

    You can add more conditions to the IF directive, such as ensure a single canonical domain without the www prefix:

    <If "req('Host') != 'myonetruesite.com' || %{HTTPS} == 'off'">
    Redirect permanent / https://myonetruesite.com/
    </If>
    

    There's a lot of familiarity inertia in using mod_rewrite for everything, but see if this works for you.

    More info: https://httpd.apache.org/docs/2.4/mod/core.html#if

    To see it in action (try without www. or https://, or with .net instead of .com): https://nohodental.com/ (a site I'm working on).

    0 讨论(0)
  • 2020-11-22 00:45

    I found a method to force all pages of my site redirect from http to analog of pages on https that work for me.

    RewriteEngine On 
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    0 讨论(0)
  • 2020-11-22 00:49

    I like this method of redirecting from http to https. Because I don't need to edit it for each site.

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    
    0 讨论(0)
提交回复
热议问题