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

前端 未结 21 867
礼貌的吻别
礼貌的吻别 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:50

    I have used this script and it works well through the site.

    if(empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "off"){
        $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        enter code hereheader('HTTP/1.1 301 Moved Permanently');
        header('Location: ' . $redirect);
        exit();
    }
    
    0 讨论(0)
  • 2020-11-28 01:51

    If you want to use PHP to do this then this way worked really well for me:

    
    <?php
    
    if(!isset($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] != "on") {
        header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"], true, 301);
        //Prevent the rest of the script from executing.
        exit;
    }
    ?>
    
    

    It checks the HTTPS variable in the $_SERVER superglobal array to see if it equal to “on”. If the variable is not equal to on.

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

    The way I've done it before is basically like what you wrote, but doesn't have any hardcoded values:

    if($_SERVER["HTTPS"] != "on")
    {
        header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
        exit();
    }
    0 讨论(0)
  • 2020-11-28 01:54

    I just created a .htaccess file and added :

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

    Simple !

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

    The PHP way:

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

    The Apache mod_rewrite way:

    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    0 讨论(0)
  • 2020-11-28 01:57

    For those using IIS adding this line in the web.config will help:

    <httpProtocol>
        <customHeaders>
            <add name="Strict-Transport-Security" value="max-age=31536000"/>
        </customHeaders>
    </httpProtocol>
    <rewrite>
        <rules>
            <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                  <match url="(.*)" />
                  <conditions>
                     <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                  </conditions>
                  <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
             </rule>
        </rules>
    </rewrite>
    

    A full example file

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <add name="Strict-Transport-Security" value="max-age=31536000"/>
                 </customHeaders>
            </httpProtocol>
            <rewrite>
                <rules>
                    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                          <match url="(.*)" />
                          <conditions>
                             <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                          </conditions>
                          <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
                     </rule>
                </rules>
           </rewrite>
       </system.webServer>
    </configuration>
    
    0 讨论(0)
提交回复
热议问题