Apache 重写规则正在应用于现有目录

Apache 重写规则正在应用于现有目录,apache,Apache,在我的apache站点配置文件中,我有以下内容。重写规则工作正常,但该规则适用于我的重写条件应该修复的现有目录。这是我的重写脚本: RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^([a-zA-Z0-9_]+)/?$ test.php?n=$1 [L] 因此,example.com/username工作得很好,使用了test.php,但是当我进

在我的apache站点配置文件中,我有以下内容。重写规则工作正常,但该规则适用于我的重写条件应该修复的现有目录。这是我的重写脚本:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_]+)/?$ test.php?n=$1 [L]
因此,
example.com/username
工作得很好,使用了
test.php
,但是当我进入一个现有的子目录,其中有一个
index.php
文件时,
example.com/myDirectory/
仍然使用test.php。知道我的剧本怎么了吗?谢谢

下面是请求
myDirectory
的重写日志,它是一个现有目录:

(2) init rewrite engine with requested uri /myDirectory/
(1) pass through /myDirectory/
(3) [perdir /var/www/] strip per-dir prefix: /var/www/myDirectory/ -> myDirectory/
(3) [perdir /var/www/] applying pattern '^tag/([a-zA-Z0-9-]+)/?$' to uri 'myDirectory/'
(3) [perdir /var/www/] strip per-dir prefix: /var/www/myDirectory/ -> myDirectory/
(3) [perdir /var/www/] applying pattern '^([a-zA-Z0-9_]+)/?$' to uri 'myDirectory/'
(2) [perdir /var/www/] rewrite 'myDirectory/' -> 'test.php?n=myDirectory'
(3) split uri=test.php?n=myDirectory -> uri=test.php, args=n=myDirectory
下面是请求
香蕉
的日志,它意味着使用重写(并且确实):


看起来这些指令在
块中。如果我将这些规则逐字地放入本地Apache配置中:

<Directory "/srv/http">
        RewriteEngine On
        RewriteCond %{SCRIPT_FILENAME} !-d
        RewriteCond %{SCRIPT_FILENAME} !-f
        RewriteRule ^([a-zA-Z0-9_]+)/?$ test.php?n=$1 [L]
</Directory>
如果我请求
/myDirectory
,我会在日志中看到以下内容:

(2) init rewrite engine with requested uri /myDirectory/
(1) pass through /myDirectory/
(3) [perdir /srv/http/] strip per-dir prefix: /srv/http/myDirectory/ -> myDirectory/
(3) [perdir /srv/http/] applying pattern '^([a-zA-Z0-9_]+)/?$' to uri 'myDirectory/'
(4) [perdir /srv/http/] RewriteCond: input='/srv/http/myDirectory/' pattern='!-d' => not-matched
(1) [perdir /srv/http/] pass through /srv/http/myDirectory/
然而,如果我请求一个不存在的目录,我会得到如下结果:

(2) init rewrite engine with requested uri /notmyDirectory/
(1) pass through /notmyDirectory/
(3) [perdir /srv/http/] add path info postfix: /srv/http/notmyDirectory -> /srv/http/notmyDirectory/
(3) [perdir /srv/http/] strip per-dir prefix: /srv/http/notmyDirectory/ -> notmyDirectory/
(3) [perdir /srv/http/] applying pattern '^([a-zA-Z0-9_]+)/?$' to uri 'notmyDirectory/'
(4) [perdir /srv/http/] RewriteCond: input='/srv/http/notmyDirectory' pattern='!-d' => matched
(4) [perdir /srv/http/] RewriteCond: input='/srv/http/notmyDirectory' pattern='!-f' => matched
(2) [perdir /srv/http/] rewrite 'notmyDirectory/' -> 'test.php?n=notmyDirectory'
所以规则正是你想要的


尝试将
RewriteLogLevel
设置为4(任何较低的值都不会显示
RewriteCond
处理)。

因此,我应该发布所有代码。我还有第二条重写规则。所以它看起来像:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^tag/([a-zA-Z0-9-]+)/?$ tag/index.php?t=$1 [L]
RewriteRule ^([a-zA-Z0-9_]+)/?$ test.php?n=$1 [L]
我不知道的是,你必须在每个规则之前应用重写条件

所以正确的方法是这样的:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^tag/([a-zA-Z0-9-]+)/?$ tag/index.php?t=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_]+)/?$ test.php?n=$1 [L]

步骤1:启用URL重写的日志记录(使用
RewriteLog
RewriteLogLevel
)并查看到底发生了什么(如果日志似乎没有立即说明问题,则发布日志)。@larsks OK,添加了日志。但我没有看到任何提示我为什么它会忽略现有的目录…嗯,它们在你的服务器上做的正是我想要的。但还是不是我的。我将
RewriteLogLevel
设置为5,奇怪的是,我的日志仍然没有显示任何高于3级的日志。我在
块中有
RewriteLog
指令,因为它不允许我将其放入
块中。也许这与此有关?对不起@larsks-我没有粘贴我所有的代码。我有一个额外的规则。答案贴在下面。非常感谢你的帮助。
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^tag/([a-zA-Z0-9-]+)/?$ tag/index.php?t=$1 [L]
RewriteRule ^([a-zA-Z0-9_]+)/?$ test.php?n=$1 [L]
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^tag/([a-zA-Z0-9-]+)/?$ tag/index.php?t=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_]+)/?$ test.php?n=$1 [L]