当前位置:   article > 正文

Apache 配置Http重定向到Https(两种方案任君选择)_阿帕奇virtualhost重定向配置

阿帕奇virtualhost重定向配置

Apache Http重定向到Https非常重要且关键,若没有对重定向进行配置,那么ssl的保护性将可以被绕过。

本文的实验环境是

ubuntu16  apache2.2

前置条件是

已经配置好了SSL证书,已经实现了使用https访问站点(若没有实现,请参考该博文

实现步骤

实现非常简单仅需3步

1、修改80端口的配置文件(两种方案任君选择)

配置文件路径为  /etc/apache2/sites-available

80端口的配置文件名称为  000-default.conf

80端口修改内容为

在<VirtualHost *:80></VirtualHost *:80>之间加入以下内容)

   关于修改方法有两个版本,建议使用版本二

版本一 (注意 www.xxxx.com、xxxx.com 中xxx要改成你的域名)
    ServerName www.xxxx.com
    ServerAlias xxxx.com
    RewriteEngine On
    RewriteRule ^/(.*?)$ https://www.xxxx.com/$1 [R]

版本二 (注意 www.xxxx.com、xxxx.com 中xxx要改成你的域名)

    ServerName www.xxxx.com
    ServerAlias xxxx.com
    RewriteEngine On
    RewriteCond %{HTTPS} !=on

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

关于版本二命令解释

RewriteEngine On是开启rewrite功能

RewriteCond %{HTTPS} !=on  为不是https的时候执行下面的规则

^(.*) https://%{SERVER_NAME}$1 [L,R] 中 ^ 匹配行的开始 

$1引用RewriteRule中的第一个正则(.*)代表的字符, %{SERVER_NAME}就是监听的网站域名

[L]:结尾标识。停止重写操作,并不再应用其他重写规则。防止本条规则被后续规则影响

 R 强制外部重定向

版本一与版本二的区别

1、版本一重定向规则都是使用硬编码,耦合性太强,建议使用版本二

2、版本二的重定向规则的正则表达式比版本一,加入了结尾表示 更加全面,也比较安全

3、我的版本二比其他晚上版本区别的地方是,原来的000-default.conf里面没有ServerName,若直接使用版本而,重启服务会失败

修改后效果

  1. <VirtualHost *:80>
  2. # The ServerName directive sets the request scheme, hostname and port that
  3. # the server uses to identify itself. This is used when creating
  4. # redirection URLs. In the context of virtual hosts, the ServerName
  5. # specifies what hostname must appear in the request's Host: header to
  6. # match this virtual host. For the default virtual host (this file) this
  7. # value is not decisive as it is used as a last resort host regardless.
  8. # However, you must set it for any further virtual host explicitly.
  9. #ServerName www.example.com
  10. #ServerAdmin webmaster@localhost
  11. DocumentRoot /var/www/html/
  12. # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
  13. # error, crit, alert, emerg.
  14. # It is also possible to configure the loglevel for particular
  15. # modules, e.g.
  16. #LogLevel info ssl:warn
  17. ErrorLog ${APACHE_LOG_DIR}/error.log
  18. CustomLog ${APACHE_LOG_DIR}/access.log combined
  19. # For most configuration files from conf-available/, which are
  20. # enabled or disabled at a global level, it is possible to
  21. # include a line for only one particular virtual host. For example the
  22. # following line enables the CGI configuration for this host only
  23. # after it has been globally disabled with "a2disconf".
  24. #Include conf-available/serve-cgi-bin.conf
  25. #加入内容
  26. RewriteEngine on
  27. ServerName www.xxxx.com
  28. ServerAlias xxxx.com
  29. RewriteEngine On
  30. RewriteRule ^/(.*?)$ https://www.xxxx.com/$1 [R]
  31. </VirtualHost>
  32. # vim: syntax=apache ts=4 sw=4 sts=4 sr noet

 

2、开启重定向模块

sudo a2enmod rewrite

3、重启服务

sudo systemctl restart apache2
 

测试

测试方法很简单,尝试使用http访问,注意地址是否会变成https


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/478145
推荐阅读
相关标签
  

闽ICP备14008679号