【问题标题】:How to force SSL on a specific file example.org/index.html?如何在特定文件 example.org/index.html 上强制 SSL?
【发布时间】:2015-08-14 07:53:39
【问题描述】:

我有一个基于 php 的网站....我尝试使用 ssl...ssl 在网站上工作正常,问题是它没有使用 https 重定向....它只显示 http 我尝试在 .htaccess 文件中进行编辑,但它不起作用.... 我的网站也以这种格式打开 example.org/index.html with /index.html

我尝试使用它,但无法正常工作

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301] 

.ht访问代码:

RewriteEngine On    
RewriteBase /
#AuthUserFile /home/products/html/path/.htpasswd
#AuthGroupFile /dev/null
#AuthName "Private Area"
#AuthType Basic
#<Limit GET POST>
#require valid-user
#</Limit>

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
    FileETag None
    <IfModule mod_headers.c>
    Header unset ETag
    Header set Cache-Control "max-age=31536000, Public"
    </IfModule>
</FilesMatch>

Options -Indexes

RewriteCond  %{REQUEST_FILENAME}  !-f

RewriteRule ^(.+).html$ $1/?%{QUERY_STRING}

-------urls---------

RewriteRule ^index/$ path/index.php
RewriteRule ^index/([^\/]+)/$ path/index.php?list=$1&%{QUERY_STRING}

【问题讨论】:

    标签: php .htaccess ssl


    【解决方案1】:

    您可以使用 PHP 进行重定向。

    我使用这个函数来检查请求是否是HTTPS:

    function isHttps() {
        return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') || $_SERVER['SERVER_PORT'] === 443;
    }
    

    现在检查请求是否不是 HTTPS,并重定向:

    if (!isHttps()) {
        header("location: https://www.example/index.php");
    }
    

    【讨论】:

    • 重定向不需要使用PHP,任何webserver配置都可以实现,更快、更简单、更安全。
    【解决方案2】:

    强制重定向到安全页面:

    if ($_SERVER['SERVER_PORT'] != '443') { 
        header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); 
        exit(); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 2018-05-08
      • 2015-03-25
      • 2018-05-06
      • 1970-01-01
      • 2016-10-21
      • 2016-08-28
      相关资源
      最近更新 更多