Apache由http自動(dòng)跳轉(zhuǎn)到https的多種方法
本文主要和大家分享Apache http自動(dòng)跳轉(zhuǎn)到https的幾種方法,當(dāng)你的站點(diǎn)使用了HTTPS之后,你可能會(huì)想把所有的HTTP請求(即端口80的請求),全部都重定向至HTTPS。這時(shí)候你可以用以下的方式來做到:
在啟用了 https 之后,還要保證之前的 http 端口可以打開,http 的 80 端口是有兩個(gè)網(wǎng)址的,所以這就導(dǎo)致需要把原來的帶 wwww 和不帶 www 的域名同時(shí)指定一個(gè) https 網(wǎng)址上面,需要做兩個(gè) Apache 的301重定向,這個(gè)其實(shí)是很簡單的,夏日博客的做法是直接在 .htaccess 文件中添加兩個(gè) 301 即可,如下所示:
rewritecond %{http_host} ^www.php.cn [nc] RewriteRule ^(.*)?$ <a target="_blank">https://www.php.cn/</a>$1 [R=301,L] RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^(.*)?$ <a target="_blank">https://www.php.cn/</a>$1 [R=301,L
第一個(gè) 301 很自然就是帶 www 的跳轉(zhuǎn)到新的 https 上面了,而下面的301重定向則是判斷如果端口不是80的話,則進(jìn)行重定向,這樣的話,帶www和不帶www的域名就一起跳轉(zhuǎn)到 https 一個(gè)網(wǎng)址上面了,當(dāng)然這種全站做301的方法是比較暴力的,通常情況下我們只要把主域名做個(gè)301就可以了,我這里是因?yàn)閱⒂昧嗽瓉淼膬蓚€(gè)域名。
PHP中文網(wǎng)還手機(jī)了一些其它的 Apache http 跳轉(zhuǎn)到 https 的方法,僅供參考:
方法1
RewriteEngine On RewriteBase / RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ <a target="_blank">https://www.php.cn/</a>$1 [R=301,L]
方法二
RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L]#整站跳轉(zhuǎn)
方法三
RewriteEngine on RewriteBase /yourfolder RewriteCond %{SERVER_PORT} !^443$ #RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L] RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] #以上至針對某個(gè)目錄跳轉(zhuǎn), yourfolder就是目錄名
方法4
redirect 301 /你的網(wǎng)頁 https://你的主機(jī)+網(wǎng)頁 #至針對某個(gè)網(wǎng)頁跳轉(zhuǎn)
方法5
RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteCond %{REQUEST_URI} !^/tz.php RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]
解釋:
%{SERVER_PORT} —— 訪問端口 %{REQUEST_URI} —— 比如如果url是 http: //localhost/tz.php,則是指 /tz.php %{SERVER_NAME} —— 比如如果url是 http: //localhost/tz.php,則是指 localhost
以上規(guī)則的意思是,如果訪問的url的端口不是443,且訪問頁面不是tz.php,則應(yīng)用RewriteRule這條規(guī)則。
這樣便實(shí)現(xiàn)了:
訪問了 http: //localhost/index.php 或者 http: //localhost/admin/index.php 等頁面的時(shí)候會(huì)自動(dòng)跳轉(zhuǎn)到 https: //localhost/index.php 或者 https: //localhost/admin/index.php,但是訪問 http: //localhost/tz.php 的時(shí)候就不會(huì)做任何跳轉(zhuǎn),也就是說 http: //localhost/tz.php 和 https: //localhost/tz.php 兩個(gè)地址都可以訪問。
PS:下面再看下Apache由http自動(dòng)跳轉(zhuǎn)到https的方法,具體內(nèi)容介紹如下所示:
修改根目錄.htaccess文件
<IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On #thinkphp去掉index.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] #http自動(dòng)跳轉(zhuǎn)到https RewriteCond %{SERVER_PORT} !^443$ #只有匹配對應(yīng)的域名才會(huì)跳轉(zhuǎn) RewriteCond %{SERVER_NAME} ^hrsc.cc|www.hrsc.cc$ RewriteRule (.*) https://%{SERVER_NAME}/$1 [R] </IfModule>
總結(jié)
以上所述是小編給大家介紹的Apache由http自動(dòng)跳轉(zhuǎn)到https的多種方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對本站網(wǎng)站的支持!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。