当你的站点使用了HTTPS以后,你可能会想把全部的HTTP请求(即端口80的请求),所有都重定向至HTTPS(即端口443)。这时候你能够用如下的方式来作到:(Apache mod_rewrite)html
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)。spa
FROM: http://www.jb51.net/article/67554.htm.net
另外一种方法:code
https://www.cnblogs.com/cnbing/p/6957157.htmlhtm