Apache 安装错误 - .htaccess 似乎无法正常工作

Apache Setup Error - .htaccess seems to not be working

好的,今天我得到了一个新的 VPS 服务器,因为有大量域指向我的旧域 - 我以前没有域指向我的新 IP。我现在已经用 ubuntu 16.04 设置了服务器 - 以前是 Centos 7。我已经尝试了一切让 .htaccccess 和我的虚拟主机工作。我在下面链接了所有文件!

.htaccess https://pastebin.ubuntu.com/25622957/
000-default.conf https://pastebin.ubuntu.com/25622958/
apache2.conf https://pastebin.ubuntu.com/25622964/

我的问题是我在这里设置错了什么,目标是我想确保是否有任何域指向我的 Origin,它会自动重写为 mydomain.com - 我也想拥有我的 Origin IP也可以拒绝访问或重写到主站点。我也不确定如何检查我的重写是否有效,因为在不同的浏览器之间会发生不同的操作。

(我在这个答案中使用了 example.com,因为 mydomain 不允许出现在 Whosebug 的帖子中)

您的 .htaccess 文件存在一些问题。首先,看这一行:

RewriteCond %{HTTP_HOST} !^http://example.com$

您在测试 {HTTP_HOST} 时不应包含 "http://",因此它应该如下所示:

RewriteCond %{HTTP_HOST} !^example.com$

其次,看这一行:

RewriteCond %{HTTPS_HOST} !^https://example.com$

我以前从未见过有人像这样测试 HTTPS,我什至在 mod_rewrite documentation 中找不到 HTTPS_HOST。我知道需要 HTTPS 的多种常用方法。这里有一些,但你只需要使用一个:

# Methods of checking the port
RewriteCond %{SERVER_PORT} !80$  #True if traffic came in on HTTP port 80
RewriteCond %{SERVER_PORT} !^443$  #True if traffic did not come in on HTTPS port 443

# Methods of checking the HTTPS status
RewriteCond %{HTTPS} off  #True if HTTPS is off
RewriteCond %{HTTPS} !=on  #True if HTTPS is not on

综上所述,以下重写规则应该可以满足您的要求:

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [R=301,L]