apache2 defaultsiteredirect但不是虚拟主机

我试图build立一个新的服务器与几个虚拟主机,但也如此,如果请求的fqdn不匹配虚拟主机,然后请求被redirect到http://example.com/log.php?url=fqdn

我已经得到了默认的主机redirect,但我定义的虚拟主机不起作用。

我正在使用不同的主机进行testing,并在命令行上使用curl -I http://hostname.example.com:8080/来读取html头文件,以直接检查redirect头,而不是使用浏览器跟踪(避免任何caching问题)。

我已经定义了一个虚拟主机作为服务器的fqdn,但是当我使用curl来请求该虚拟主机时,我得到了redirect。 如果我通过没有定义虚拟主机的其他名称来请求服务器,我也会被redirect。

ubuntu上的apache版本是2.2.16

configuration(从几个不同的文件按顺序连接在一起)如下所示:

 Listen 8080 NameVirtualHost * <VirtualHost _default_> ServerAdmin [email protected] RewriteEngine On RewriteRule ^(.*)$ http://example.com/log.php?url=%{HTTP_HOST}$1 [R=302,L] </VirtualHost> <VirtualHost *> <Directory "/var/www"> allow from all Options Indexes </Directory> DocumentRoot /var/www ServerName hostname.example.com </VirtualHost> 

我也尝试了hostname.example.com:*和hostname.example.com:8080的ServerName值


如果我不够清楚:

anything.anything.anything.any / something requests from my server should redirect to example.com/log.php?url=anything.anything.any/something

从我的服务器请求的foo.example.com(未定义为VirtualHost)应该redirect到example.com/log.php?url=foo.example.com

hostname.example.com(定义为VirtualHost)从我的服务器请求应该返回一个HTML文档

从我的服务器请求的anothername.example.com(也定义为VirtualHost)应该返回一个html文档


事实certificate,因为服务器自己的fqdn是hostname.example.com,即使有一个名为VirtualHost,它也会被redirect到Default VirtualHost。 其他fqdn与服务器fqdn不一样的工作,我打算。

解决scheme来实现我最初想要的是服务器的fqdn应该显示一个页面,但任何没有名称VirtualHost定义的fqdn将被redirect如上是很简单,一旦我意识到问题是什么。

由于服务器的fqdn总是显示默认的VirtualHost,而不pipe是否为fqdn定义了任何名为VirtualHost,这意味着解决scheme必须写入默认的VirtualHost。

因此,我添加了重写条件,如果主机名与我的服务器fqdn不匹配,则应用原始重写规则。 此外,因为我使用非标准端口,所以有必要在条件中定义端口,因为%{HTTP_HOST}包含端口号。 我通过在fqdn之后select允许任何端口号来做到这一点,因为我稍后可能会更改服务器的端口。

 <VirtualHost _default_> ServerAdmin [email protected] RewriteEngine On RewriteCond %{HTTP_HOST} !(^hostname\.example\.com[:0-9]*$) [NC] RewriteRule ^(.*)$ http://example.com/log.php?url=%{HTTP_HOST}$1 [R=302,L] <Directory "/var/www"> allow from all Options Indexes </Directory> DocumentRoot /var/www </VirtualHost> 

这现在与其他命名VirtualHosts一起工作。