2012-12-10 50 views
0

在我的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 

这里是日志请求bananas这是指使用重写(并执行):

(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 
+1

第1步:打开网址的记录改写(以'RewriteLog'和'RewriteLogLevel'),看看到底发生了什么(如果发布日志,他们不似乎没有立即说明问题)。 – larsks

+0

@larsks好的,添加它们。但是我没有看到任何让我想起为什么忽略现有目录的东西...... – inorganik

回答

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。 I found that answer here.

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

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] 
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处理)。

+0

那么,他们正在做你想要的服务器。但仍然不是我的。我将'RewriteLogLevel'设置为5,奇怪的是,我的日志仍然没有显示高于3级的任何日志。我在''块中有'RewriteLog'指令,因为它不会让我把它放在' '块。也许这跟它有关系? – inorganik

+0

对不起@larsks - 我没有粘贴我的所有代码。我有一个额外的规则。下面发布答案。非常感谢你的帮助。 – inorganik