欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Apache (htaccess) 强制 HTTP 全部跳转到 HTTPS 自己亲自做的 有大用 有大大用

下面三行是自己亲自做的 有大用

RewriteCond %{SERVER_PORT} 80

RewriteCond %{HTTP_HOST} wap.shrszg.cn$ [NC]

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]



Apache 强制 HTTP 全部跳转到 HTTPS

2018年01月06日 01:14:42 阳光岛主 阅读数:17918                
 版权声明:本文为米扑博客原创文章,未经米扑博客允许不得转载。 https://blog.csdn.net/sunboy_2050/article/details/78986266

米扑博客最新写了一篇博客《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 yanggang_2050@163.com                                                
    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/                

 

       
       
       
  • weixin_42375443

    weixin_42375443: 问一下,[L,R] 这直接写R 跟 R=301 有什么区别(7个月前#2楼)                        
    0                        
  • only_lamp

    vijayqi: 老哥,稳得一匹,谢谢(9个月前#1楼)                        


来自   https://blog.csdn.net/ithomer/article/details/78986266

普通分类: