0

In my apache site configuration file, I have the following. The rewrite rule works fine, but the rule is applying to existing directories which my rewrite conditions were supposed to fix. Here is my rewrite script:

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

So example.com/username works great and uses test.php, but also when I enter an existing sub-directory that has an index.php file in it, aka, example.com/myDirectory/ still uses test.php. Any idea what's wrong with my script? Thanks.

Below is a rewrite log for request myDirectory which is an existing directory:

(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

And here is the log for request bananas which is meant to use the rewrite (and does):

(2) init rewrite engine with requested uri /bananas
(1) pass through /bananas
(3) [perdir /var/www/] strip per-dir prefix: /var/www/bananas -> bananas
(3) [perdir /var/www/] applying pattern '^tag/([a-zA-Z0-9-]+)/?$' to uri 'bananas'
(3) [perdir /var/www/] strip per-dir prefix: /var/www/bananas -> bananas
(3) [perdir /var/www/] applying pattern '^([a-zA-Z0-9_]+)/?$' to uri 'bananas'
(2) [perdir /var/www/] rewrite 'bananas' -> 'test.php?n=bananas'
(3) split uri=test.php?n=bananas -> uri=test.php, args=n=bananas
4

2 回答 2

0

所以,我应该发布我所有的代码。我在那里有第二个重写规则。所以它看起来像:

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]

我不知道的是,您必须在每条规则之前应用 RewriteCond。我在这里找到了答案。

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

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]
于 2012-12-11T20:03:43.353 回答
0

看起来你在一个<Directory>块内有这些指令。如果我将这些规则逐字逐句放入我的本地 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>

并确保RewriteLogLevel>=4:

RewriteLogLevel 4

如果我请求,我会在日志中看到以下内容/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处理)。

于 2012-12-11T18:50:51.580 回答