Apache mod_rewrite实现HTTP和HTTPS重定向跳转

时间:2017-06-08

当你的站点使用了HTTPS之后,你可能会想把所有的HTTP请求(即端口80的请求),全部都重定向至HTTPS(即端口443)。这时候你可以用以下的方式来做到:(Apache mod_rewrite)

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteCond %{SERVER_PORT} 80
 RewriteRule ^(.*)$ https://www.genban.org/$1 [R=301,L]
</IfModule>

把这段代码放在.htaccess文件,即可实现HTTP到HTTPS的重定向。

而当你又想用回HTTP的时候,反过来就可以了:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteCond %{SERVER_PORT} 443
 RewriteRule ^(.*)$ http://www.genban.org/$1 [R=301,L]
</IfModule>

其中R=301表示Moved Permanently,即告诉搜索引擎或者浏览器下去直接访问后者的地址,如果只是试验性地重定向,可以使用R=302(Found)。

上一条:Apache启动提示错误undefined symbol: libiconv_open解决方法 下一条:在Apache服务器上安装SSL支持的教程

相关文章

最新文章