How can I force users to access my page over HTTPS instead of HTTP?

前端 未结 21 869
礼貌的吻别
礼貌的吻别 2020-11-28 01:15

I\'ve got just one page that I want to force to be accessed as an HTTPS page (PHP on Apache). How do I do this without making the whole directory require HTTPS? Or, if you s

相关标签:
21条回答
  • 2020-11-28 01:44

    You could do it with a directive and mod_rewrite on Apache:

    <Location /buyCrap.php>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    </Location>
    

    You could make the Location smarter over time using regular expressions if you want.

    0 讨论(0)
  • 2020-11-28 01:46

    Had to do something like this when running behind a load balancer. Hat tip https://stackoverflow.com/a/16076965/766172

    function isSecure() {
        return (
            (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
         || $_SERVER['SERVER_PORT'] == 443
         || (
                (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
             || (!empty($_SERVER['HTTP_X_FORWARDED_SSL'])   && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on')
            )
        );
    }
    
    function requireHTTPS() {
        if (!isSecure()) {
            header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], TRUE, 301);
            exit;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 01:47

    http://www.besthostratings.com/articles/force-ssl-htaccess.html

    Sometimes you may need to make sure that the user is browsing your site over securte connection. An easy to way to always redirect the user to secure connection (https://) can be accomplished with a .htaccess file containing the following lines:

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

    Please, note that the .htaccess should be located in the web site main folder.

    In case you wish to force HTTPS for a particular folder you can use:

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

    The .htaccess file should be placed in the folder where you need to force HTTPS.

    0 讨论(0)
  • 2020-11-28 01:47

    Using this is NOT enough:

    if($_SERVER["HTTPS"] != "on")
    {
        header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
        exit();
    }
    

    If you have any http content (like an external http image source), the browser will detect a possible threat. So be sure all your ref and src inside your code are https

    0 讨论(0)
  • 2020-11-28 01:48

    maybe this one can help, you, that's how I did for my website, it works like a charm :

    $protocol = $_SERVER["HTTP_CF_VISITOR"];
    
    if (!strstr($protocol, 'https')){
        header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
        exit();
    }
    
    0 讨论(0)
  • 2020-11-28 01:49

    Ok.. Now there is tons of stuff on this now but no one really completes the "Secure" question. For me it is rediculous to use something that is insecure.

    Unless you use it as bait.

    $_SERVER propagation can be changed at the will of someone who knows how.

    Also as Sazzad Tushar Khan and the thebigjc stated you can also use httaccess to do this and there are a lot of answers here containing it.

    Just add:

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

    to the end of what you have in your .httaccess and thats that.

    Still we are not as secure as we possibly can be with these 2 tools.

    The rest is simple. If there are missing attributes ie...

    if(empty($_SERVER["HTTPS"])){ // SOMETHING IS FISHY
    }
    
    if(strstr($_SERVER['HTTP_HOST'],"mywebsite.com") === FALSE){// Something is FISHY
    }
    


    Also say you have updated your httaccess file and you check:

    if($_SERVER["HTTPS"] !== "on"){// Something is fishy
    }
    

    There are a lot more variables you can check ie..

    HOST_URI (If there are static atributes about it to check)

    HTTP_USER_AGENT (Same session different values)

    So all Im saying is dont just settle for one or the other when the answer lies in a combination.

    For more httaccess rewriting info see the docs-> http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

    Some Stacks here -> Force SSL/https using .htaccess and mod_rewrite
    and
    Getting the full URL of the current page (PHP)
    to name a couple.

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