使用.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( '