【问题标题】:Non www to https://www非 www 到 https://www
【发布时间】:2018-10-13 14:54:06
【问题描述】:

我在 ubuntu 16.04 中运行 NodeJs 应用程序,我希望请求:http://http://www 重定向到 https://www

我尝试通过像这样编辑/etc/apache2/sites-available/000-default.conf 来实现这一点:

<VirtualHost *:80>
    ServerAdmin admin@example.net
    ServerName www.example.net
    ServerAlias example.net www.example.net

    ErrorLog /var/log/apache2/mysite-error_log
    TransferLog /var/log/apache2/mysite-access_log

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.example.net$ [NC]
    RewriteCond %{HTTP_HOST} !^$
    RewriteRule (.+) http://www.example.net$1 [R=301,L]
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

注意:这里是 example.net,我正在写我的域。

【问题讨论】:

    标签: node.js apache ubuntu apache2


    【解决方案1】:

    在端口 80 的 VirtualHost 中

    几个点:

    1) 第二个RewriteCond 不是必需的,如果您访问虚拟主机,%{HTTP_HOST} 将始终设置为某个值。

    2)ServerAlias example.net www.example.net。去掉www.example.net,ServerName里已经设置好了。

    3)为了帮助调试Apache,你可以把LogLevel debug 再试一次。

    4) 你的RewriteRule (.+) http://www.example.net$1 [R=301,L] 实际上应该是

    RewriteRule ^(.*)$ https://www.example.net$1 [R=301,L]
    

    你的规则:

    1. 重定向到 http。
    2. (.+):表示某个字符,1到n次。你什么都没有(比如http://www.example.com,你的规则永远不会匹配。

    新规则将确保在将请求的域添加到 https 重定向之后,无论是否存在。

    在端口 443 的 VirtualHost 中

    在443端口,流量已经是https了,所以只需要加“www”:

    RewriteCond %{HTTP_HOST} ^example.net$ [NC]
    RewriteRule ^(.*)$ https://www.example.net$1 [R=301,L]
    

    这只会在请求的域为https://example.net 时添加“www”。

    【讨论】:

    • 我已经完成了你所说的一切,好消息是它会将所有请求重定向到www,但现在我面临“重定向你太多次”。当我试图打开网络时。
    • 此重写规则进入端口 80 的 VirtualHost。在端口 443 中,不要放置完全相同的重定向。在答案中查看我的编辑。
    猜你喜欢
    • 2014-12-16
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 2015-02-13
    相关资源
    最近更新 更多