首页技术博客 服务器运维 正文

apache环境下实现网站http访问301跳转到https

2018-03-12 2082 0条评论

打开网站的配置文件:XXX.conf,

在<VirtualHost *:80>下面添加以下代码;
RewriteEngine on
RewriteRule ^(.*)$ https://blog.haozi.org$1 [R=301,L]

将代码中的网址换成自己的网址就可以了。

配置完成后,重启Apache即可。


下面规则中蜗牛使用的域名是blog.haozi.org或haozi.org,使用时请根据自己需要更改为自己的域名。至于规则放置到什么位置,一般我们可以选择放到到apache的conf配置文件对应位置,或指定站点目录 .htaccess 文件内容的头部。

1、强制HTTPS方式访问,对WWW或顶级域名不做跳转。

RewriteEngine OnRewriteCond %{SERVER_PORT} 80RewriteRule ^(.*)$ https://blog.haozi.org/$1 [L,R=301]

2、强制HTTPS方式访问,并自动将顶级域名跳转到WWW。

RewriteEngine OnRewriteCond %{HTTP_HOST} !^blog.haozi.org$ [NC]RewriteRule ^(.*)$ https://blog.haozi.org/$1 [L,R=301]RewriteCond %{SERVER_PORT} 80RewriteRule ^(.*)$ https://blog.haozi.org/$1 [L,R=301]

3、强制HTTPS方式访问,并自动将WWW跳转到顶级域名。

RewriteEngine OnRewriteCond %{HTTP_HOST} !^haozi.org$ [NC]RewriteRule ^(.*)$ https://haozi.org/$1 [L,R=301]RewriteCond %{SERVER_PORT} 80RewriteRule ^(.*)$ https://haozi.org/$1 [L,R=301]


文章版权及转载声明

本文作者:符文浩 网址:http://blog.fuwenhao.com/post/60.html 发布于 2018-03-12
文章转载或复制请以超链接形式并注明出处。

发表评论

快捷回复:

评论列表 (暂无评论,2082人围观)参与讨论

还没有评论,来说两句吧...

取消
微信二维码
微信二维码
支付宝二维码