您现在的位置是:首页> 操作系统> Linux> apache

Apache环境下http强制跳转https的方法

  • 4678人已阅读
  • 时间:2018-10-16 09:04:20
  • 分类:apache
  • 作者:祥哥

当网站开启HTTS之后,我们肯定是想把HTTP 80强制转HTTPS访问了.

祥哥做二种方法:

第一种方法:

.htaccess配置

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

或者

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]

第二种方法:

配置Apache的配置文件,例如httpd-vhosts.conf

<VirtualHost *:80>

    ServerAdmin sy_yjx@188.com
    DocumentRoot "/home/www"
    ServerName www.wuaizhongyi.com
    ServerAlias wuaizhongyi.com
    RewriteEngine on
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*) https://%{SERVER_NAME}$1  [L,R]
    <Directory "/home/www">
        AllowOverride ALL
        Options -Indexes +FollowSymlinks
        Require all granted\
    </Directory>
    ErrorLog "| /usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/%Y%m%dzhonyi-error_log 86400 480"
    CustomLog "| /usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/%Y%m%dzhongyi-access_log 86400 480"common
</VirtualHost>


Top