Force SSL/https using .htaccess and mod_rewrite

后端 未结 9 1659
一向
一向 2020-11-22 05:24

How can I force to SSL/https using .htaccess and mod_rewrite page specific in PHP.

相关标签:
9条回答
  • 2020-11-22 06:04

    For Apache, you can use mod_ssl to force SSL with the SSLRequireSSL Directive:

    This directive forbids access unless HTTP over SSL (i.e. HTTPS) is enabled for the current connection. This is very handy inside the SSL-enabled virtual host or directories for defending against configuration errors that expose stuff that should be protected. When this directive is present all requests are denied which are not using SSL.

    This will not do a redirect to https though. To redirect, try the following with mod_rewrite in your .htaccess file

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    or any of the various approaches given at

    • http://www.askapache.com/htaccess/http-https-rewriterule-redirect.html

    You can also solve this from within PHP in case your provider has disabled .htaccess (which is unlikely since you asked for it, but anyway)

    if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
        if(!headers_sent()) {
            header("Status: 301 Moved Permanently");
            header(sprintf(
                'Location: https://%s%s',
                $_SERVER['HTTP_HOST'],
                $_SERVER['REQUEST_URI']
            ));
            exit();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:06

    I'd just like to point out that Apache has the worst inheritance rules when using multiple .htaccess files across directory depths. Two key pitfalls:

    • Only the rules contained in the deepest .htaccess file will be performed by default. You must specify the RewriteOptions InheritDownBefore directive (or similar) to change this. (see question)
    • The pattern is applied to the file path relative to the subdirectory and not the upper directory containing the .htaccess file with the given rule. (see discussion)

    This means the suggested global solution on the Apache Wiki does not work if you use any other .htaccess files in subdirectories. I wrote a modified version that does:

    RewriteEngine On
    # This will enable the Rewrite capabilities
    
    RewriteOptions InheritDownBefore
    # This prevents the rule from being overrided by .htaccess files in subdirectories.
    
    RewriteCond %{HTTPS} !=on
    # This checks to make sure the connection is not already HTTPS
    
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [QSA,R,L]
    # This rule will redirect users from their original location, to the same location but using HTTPS.
    # i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
    
    0 讨论(0)
  • 2020-11-22 06:09

    This code works for me

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP:X-HTTPS} !1
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
    
    0 讨论(0)
提交回复
热议问题