使用.htaccess和mod_rewrite强制使用SSL/https如何使用.htaccess和PHP中特定的mod_rewrite页面强制使用SSL/https。
3 回答
![?](http://img1.sycdn.imooc.com/5333a2320001acdd02000200-100-100.jpg)
泛舟湖上清波郎朗
TA贡献1818条经验 获得超3个赞
mod_rewrite
RewriteEngine On# If we receive a forwarded http request from a proxy...RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR] # ...or just a plain old http request directly from the clientRewriteCond %{HTTP:X-Forwarded-Proto} =""RewriteCond %{HTTPS} !=on # Redirect to https versionRewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
![?](http://img1.sycdn.imooc.com/54584d6100015f5802200220-100-100.jpg)
繁星点点滴滴
TA贡献1803条经验 获得超3个赞
PHP解决方案
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( '<script type="javascript">document.location.href="'.$httpsURL.'";</script>' ); } }}
require()
forceHTTPS()
HTACCESS/MOD重写解
RewriteEngine on# Check for POST SubmissionRewriteCond %{REQUEST_METHOD} !^POST$# Forcing HTTPSRewriteCond %{HTTPS} !=on [OR]RewriteCond %{SERVER_PORT} 80# Pages to ApplyRewriteCond %{REQUEST_URI} ^something_secure [OR]RewriteCond %{REQUEST_URI} ^something_else_secureRewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]# Forcing HTTPRewriteCond %{HTTPS} =on [OR]RewriteCond %{SERVER_PORT} 443 # Pages to ApplyRewriteCond %{REQUEST_URI} ^something_public [OR]RewriteCond %{REQUEST_URI} ^something_else_publicRewriteRule . * http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
- 3 回答
- 0 关注
- 676 浏览
添加回答
举报
0/150
提交
取消