當你的站點使用了HTTPS之后,你可能會想把所有的HTTP請求(即端口80的請求),全部都重定向至HTTPS(即端口443)。這時候你可以用以下的方式來做到:(Apache mod_rewrite)
|
1
2
3
4
5
6
|
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://jb51.net/$1 [R=301,L]
</IfModule>
|
把這段代碼放在.htaccess文件,即可實現HTTP到HTTPS的重定向。
而當你又想用回HTTP的時候,反過來就可以了:
|
1
2
3
4
5
6
|
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://jb51.net/$1 [R=301,L]
</IfModule>
|
其中R=301表示Moved Permanently,即告訴搜索引擎或者瀏覽器下去直接訪問后者的地址,如果只是試驗性地重定向,可以使用R=302(Found)。
FROM: http://www.jb51.net/article/67554.htm
另一種方法:
https://www.cnblogs.com/cnbing/p/6957157.html
