RewriteCond 总是匹配不正确

RewriteCond 总是匹配不正确

如果我写:

<?php
echo "SCRIPT_NAME: ".@$_SERVER['SCRIPT_NAME']."<br />";
?>

脚本名称:/index.php

显示的是上面那行。

我正在使用这些重写行

RewriteCond %{SCRIPT_NAME} !^/index\.php$
RewriteRule .* http://example.com/404 [L]

我检查过:

http://example.com/foo

http://example.com/bar

http://example.com/hdhd

RewriteCond 匹配,然后我被重定向到 404

[03/Aug/2013:13:07:48 +0200] [example.com/sid#23c3710][rid#263afe8/initial] (4) [perdir /var/www/vhosts/example.com/httpdocs/] RewriteCond: input='' pattern='!^/index\\.php$' => matched
[03/Aug/2013:13:07:48 +0200] [example.com/sid#23c3710][rid#263afe8/initial] (2) [perdir /var/www/vhosts/example.com/httpdocs/] rewrite '404' -> 'http://example.com/404'
[03/Aug/2013:13:07:48 +0200] [example.com/sid#23c3710][rid#263afe8/initial] (2) [perdir /var/www/vhosts/example.com/httpdocs/] implicitly forcing redirect (rc=302) with http://example.com/404
[03/Aug/2013:13:07:48 +0200] [example.com/sid#23c3710][rid#263afe8/initial] (1) [perdir /var/www/vhosts/example.com/httpdocs/] escaping http://example.com/404 for redirect
[03/Aug/2013:13:07:48 +0200] [example.com/sid#23c3710][rid#263afe8/initial] (1) [perdir /var/www/vhosts/example.com/httpdocs/] redirect to http://example.com/404 [REDIRECT/302]

但是如果我使用 example.com/index.php/nnn 或 example.com/index.php

我仍在被重定向

[03/Aug/2013:13:10:41 +0200] [example.com/sid#23c3710][rid#263eff8/initial] (4) [perdir /var/www/vhosts/example.com/httpdocs/] RewriteCond: input='' pattern='!^/index\\.php$' => matched
[03/Aug/2013:13:10:41 +0200] [example.com/sid#23c3710][rid#263eff8/initial] (2) [perdir /var/www/vhosts/example.com/httpdocs/] rewrite '404' -> 'http://example.com/404'
[03/Aug/2013:13:10:41 +0200] [example.com/sid#23c3710][rid#263eff8/initial] (2) [perdir /var/www/vhosts/example.com/httpdocs/] implicitly forcing redirect (rc=302) with http://example.com/404
[03/Aug/2013:13:10:41 +0200] [example.com/sid#23c3710][rid#263eff8/initial] (1) [perdir /var/www/vhosts/example.com/httpdocs/] escaping http://example.com/404 for redirect
[03/Aug/2013:13:10:41 +0200] [example.com/sid#23c3710][rid#263eff8/initial] (1) [perdir /var/www/vhosts/example.com/httpdocs/] redirect to http://example.com/404 [REDIRECT/302

我在 input='' 处看不到任何内容,每次请求时它都是空的。然后 never 会像模式一样不同,然后 always 会匹配

有什么建议吗?

答案1

您想要的 Apache 变量称为%{SCRI­PT_­FIL­ENAME}

在 PHP 的$_SERVER超级全局变量中,SCRIPT_NAME和都SCRIPT_FILENAME存在,但在 Apache 中,仅%{SCRIPT_FILENAME}存在。

目前尚不清楚文档每个 PHP 变量都包含确切的内容,但在我的测试中,SCRIPT_FILENAME是一个完整的文件系统路径,并且SCRIPT_NAME是来自文档根目录的路径或 URL 的路径部分。

相关内容