Apache 強制 HTTP 全部跳轉到 HTTPS

米撲博客最新寫了一篇博客《Apache 強制 HTTP 全部跳轉到 HTTPS》,分享出來

更多經典技術博客,請見我的米撲博客:https://blog.mimvp.com


.htaccess 在每一層獨立服務根目錄下都存在,例如:

全部網站根目錄爲   /var/www/html/.htaccess

米撲博客根目錄位   /var/www/html/mimvp-wordpress/.htaccess

米撲論壇根目錄位   /var/www/html/mimvp-discuz/.htaccess

米撲學習根目錄位   /var/www/html/mimvp-study/.htaccess

 

HTTP 80 強制轉 HTTPS

全站採用https協議訪問,所以需要http重定向到https,只需要在.htaccess加入下面規則

在相應的網站根目錄新建 .htaccess

例如,在米撲博客的網站根目錄下,新建   

vim   /var/www/html/mimvp-wordpress/.htaccess

1
2
3
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

或者

1
2
3
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]

 

 

強制301重定向 HTTPS

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]
</IfModule>

 

站點綁定多個域名

只允許www.gworg.com 跳轉

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

###把網址更改爲自己的###

 

高級用法 (可選)

RewriteEngine on
# 強制HTTPS
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{SERVER_PORT} 80
# 某些頁面強制
RewriteCond %{REQUEST_URI} ^something_secure [OR]
RewriteCond %{REQUEST_URI} ^something_else_secure
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# 強制HTTP
RewriteCond %{HTTPS} =on [OR]
RewriteCond %{SERVER_PORT} 443
# 某些頁面強制
RewriteCond %{REQUEST_URI} ^something_public [OR]
RewriteCond %{REQUEST_URI} ^something_else_public
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

 

 

Apache mod_rewrite實現HTTP和HTTPS重定向跳轉

當你的站點使用了HTTPS之後,你可能會想把所有的HTTP請求(即端口80的請求),全部都重定向至HTTPS(即端口443)。這時候你可以用以下的方式來做到:(Apache mod_rewrite)

把這段代碼放在.htaccess文件,即可實現HTTP到HTTPS的重定向。

1
2
3
4
5
6
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteCond %{SERVER_PORT} 80
 RewriteRule ^(.*)$ https://blog.mimvp.com/$1 [R=301,L]
</IfModule>

 

而當你又想用回HTTP的時候,反過來就可以了:

1
2
3
4
5
6
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteCond %{SERVER_PORT} 443
 RewriteRule ^(.*)$ https://blog.mimvp.com/$1 [R=301,L]
</IfModule>

其中R=301表示Moved Permanently,即告訴搜索引擎或者瀏覽器下去直接訪問後者的地址,

如果只是試驗性地重定向,可以使用R=302(Found),臨時跳轉

更多30x狀態,請見米撲博客HTTP協議中POST、GET、HEAD、PUT等請求方法總結

 

VirtualHost 添加重定向

實測以上方法,對於我的需求場景,都無效

我的項目場景:

1. 在我的根目錄下 /var/www/htmp/

2. 配置有多個網站,如米撲博客(/var/www/htmp/mimvp-blog/)、米撲論壇(/var/www/htmp/mimvp-forum/)、米撲學習(/var/www/htmp/mimvp-study/)等

3. 對於米撲博客的http請求,全部定向到https博客;對於米撲論壇的http請求,全部定向到https論壇;

最後,解決方案是在 VirtualHost 節點裏,添加如下配置:

    RewriteEngine on
    RewriteCond   %{HTTPS} !=on
    RewriteRule   ^(.*)  https://%{SERVER_NAME}$1 [L,R]

完整配置參數如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# blog
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/wordpress
    ServerName blog.mimvp.com
 
    RewriteEngine on
    RewriteCond   %{HTTPS} !=on
    RewriteRule   ^(.*)  https://%{SERVER_NAME}$1 [L,R]
 
    DirectoryIndex index.php
    ErrorLog /var/log/blog.mimvp.com-error_log
    CustomLog /var/log/blog.mimvp.com-access_log common
</VirtualHost>

 

在米撲論壇、米撲學習等 VirtualHost 節點裏,都添加如上配置,問題解決。

米撲博客效果,全部自動跳轉到 https :

https://blog.mimvp.com

https://blog.mimvp.com/about/

 

發佈了747 篇原創文章 · 獲贊 3583 · 訪問量 1412萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章