如何在特定文件example.org/index.html上强制使用SSL?

时间:2015-05-31 23:03:43

标签: php .htaccess ssl

我有一个基于PHP的网站....我试图使用ssl ... ssl在网站工作正常,问题是它没有重定向与https ....它只显示http 我尝试编辑.htaccess文件,但它不工作.... 我的网站也使用此格式example.org/ index.html 打开 /index.html

我试图使用它,但没有工作

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

.htaccess代码:

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}

2 个答案:

答案 0 :(得分:0)

您可以使用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");
}

答案 1 :(得分:0)

强制重定向到安全页面:

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