0

我在 ubuntu 16.04 中运行 NodeJs 应用程序,我想要 requests: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,我正在写我的域。

4

1 回答 1

0

在端口 80 的 VirtualHost 中

几点:

1)第二个RewriteCond不是必需的,%{HTTP_HOST}如果您访问 VirtualHost,将始终设置为某些内容。

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]

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

于 2018-10-14T02:17:20.740 回答