1

在我的服务器上,我正在运行awstats,这是一个我目前可以通过以下 URL 访问的脚本:

https://stats.example.com/bin/awstats.pl/?config=global

我正在尝试使用重写规则,以便我可以使用

https://stats.example.com/global

这是我为重写规则定义的

RewriteRule ^(.*)$ bin/awstats.pl/?config=$1 [NC,L]

httpd 虚拟主机

# Address
ServerName              stats.example.com

# Rewrite
RewriteRule      ^(.*)$ bin/awstats.pl/?config=$1 [NC,L]

Options                 ExecCGI
AddHandler              cgi-script .cgi .pl
Alias                   /awstatsstuff "/path/to/awstatsstuff/"

<Directory "/path/to/awstatsstuff">
        Options ExecCGI
        AllowOverride None
        Order allow,deny
        Allow from all
</Directory>

问题是我尝试访问的任何内容(除了索引)都会给我一个 400,并且我的 apache 日志显示没有错误。

如果此规则正常工作,我是否有不同的配置问题?还是我错过了什么?是的,RewriteEngine开着。


编辑

根据 Michael Berkowski 的评论,我确定这实际上资源也被定向到 pl 脚本的问题,我已经修改并正在使用以下内容:

RewriteCond             %{REQUEST_FILENAME} !-d
RewriteCond             %{REQUEST_FILENAME} !-f
RewriteRule             ^/([0-9a-z]+\.[0-9a-z]+\.[0-9a-z]+)$    bin/awstats.pl/?config=$1 [NC,L]

我现在可以使用再次加载页面

https://stats.example.com/bin/awstats.pl/?config=www.example.com

这意味着可以正确加载所有资源,但是

https://stats.example.com/www.exmaple.com

将返回 400 (这不是来自 pl 脚本,如果找不到指定的配置文件,它将返回 200 和错误消息,同样,日志中没有错误消息。


另一个编辑

在更改[NC,L]为 时[R=302],我会根据要求提供正确的重定向,

curl -k "https://stats.example.com/a.b.c"
...
<p>The document has moved <a href="https://stats.example.com/bin/awstats.pl/?config=a.b.c">here</a>.</p>
...

使用[R=403]证明重写规则按预期工作

我现在面临的问题是,在使用时[NC,L],我仍然收到一个400,httpd 日志中没有可用的错误。

4

1 回答 1

1

我强烈怀疑对索引以外的文档的请求被错误地捕获(.*)并错误地发送到config=。400(错误请求)可能是由于 awstats 超出了它无法在那里处理的值而导致的。

应该做两件事。首先,您需要从重写中排除真实存在的文件和目录,通常使用一对RewriteCond. 然后,代替非常通用(.*)的匹配器,使用更具体到实际应该被认为对 有效的值的匹配器config=

# If the requested document is not a known
# file or directory on disk...
RewriteCond %{REQUEST_FILENAME} !=f
RewriteCond %{REQUEST_FILENAME} !=d

# Rewrite patterns matching only the expected
# config= values for awstats
RewriteRule ^([a-z0-9]+\.[a-z0-9]+\.[a-z0-9]+)$ bin/awstats.pl?config=$1 [L,NC]

上面我曾经[a-z0-9]+\.匹配评论线程中提到的 3 部分 FQDN 字符串。这可能需要额外的改进。"global"例如,为了也支持字符串,您可以将其扩展为

RewriteRule ^(global|[a-z0-9]+\.[a-z0-9]+\.[a-z0-9]+)$ bin/awstats.pl?config=$1 [L,NC]
于 2016-01-20T02:59:36.417 回答