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

时间:2017-09-26 21:11:46

标签: apache .htaccess mod-rewrite ubuntu-16.04

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

.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也拒绝访问或重写到主站点。我也不确定如何检查我的重写是否在不同的浏览器之间发生不同的操作。

1 个答案:

答案 0 :(得分:1)

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

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

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

你不应该包括" http://"当你测试{HTTP_HOST}时,它应该是这样读的:

RewriteCond %{HTTP_HOST} !^example.com$

其次,看看这一行:

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

我以前从未见过有人像这样测试过HTTPS,我甚至无法在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]
相关问题