2012-12-10 51 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