加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

使用.htaccess和mod_rewrite强制SSL / https

发布时间:2020-05-25 02:31:49 所属栏目:PHP 来源:互联网
导读:本文章向大家介绍使用.htaccess和mod_rewrite强制SSL / https,需要的朋友可以参考一下。

需求:

如何使用PHP中特定的.htaccess和mod_rewrite页面强制使用SSL / https。

PHP解决方案

直接借用Gordon非常全面的答案,我注意到你的问题提到强制HTTPS / SSL连接是特定于页面的。

function forceHTTPS(){

$httpsURL = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

if( count( $_POST )>0 )

die( 'Page should be accessed with HTTPS,but a POST Submission has been sent here. Adjust the form to point to '.$httpsURL );

if( !isset( $_SERVER['HTTPS'] ) || $_SERVER['HTTPS']!=='on' ){

if( !headers_sent() ){

header( "Status: 301 Moved Permanently" );

header( "Location: $httpsURL" );

exit();

}else{

die( '