裸域自动重定向但不是www

时间:2017-11-30 17:44:57

标签: apache .htaccess http redirect https

我想将流量重定向到HTTPS

来自myawesomesite.com它重定向到https://myawesomesite.com,来自http(s)://www.myawesomesite.com,根据来电,它会转到HTTPHTTPS

出了什么问题? 我的虚拟主机文件是:

<VirtualHost *:80>
        DocumentRoot /var/www/myawesomesite/public
    ServerName myawesomesite.com
    ServerAlias www.myawesomesite.com
        <Directory "/var/www/myawesomesite/public/">
                AllowOverride All
                Options FollowSymLinks
        </Directory>
    ErrorLog /var/log/apache2/myawesomesite-error_log
    RewriteEngine On
    RewriteCond %{SERVER_NAME} ="myawesomesite.com" [OR]
    RewriteCond %{SERVER_NAME} ="www.myawesomesite.com"
    RewriteRule (.*) https://%{SERVER_NAME}%{REQUEST_URI}
</VirtualHost>

1 个答案:

答案 0 :(得分:1)

这是通过重写功能强制SSL的正确方法:

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

当然,删除您当前的代码。享受!

更新

你也可以这样做更好:

<virtualhost *:80="">
ServerName www.example.com
Redirect / https://www.example.com/
</virtualhost>