2009-11-08 96 views
0

目前,我已經安裝在我的htaccess使example.com/dept/noc去:使用htaccess的條件+ URL重寫

RewriteEngine On 
RewriteRule ^([A-Za-z]+)/([A-Za-z]+)$ index.php?dept=$1&n=$2 [QSA] 
RewriteCond %{SERVER_PORT} 80 
example.com/index.php?dept=dept & N = NOC

我有一個名爲上傳文件夾,我將如何確保URL重寫是不是在爲/上傳/文件夾的效果(例外),因爲

example.com/upload/file.doc不起作用?

+0

添加的RewriteCond與例外? – axk

回答

3

嘗試:

RewriteEngine On 
RewriteCond %{REQUEST_URI} !^/upload/.* 
RewriteRule ^([A-Za-z]+)/([A-Za-z]+)$ index.php?dept=$1&n=$2 [QSA] 
RewriteCond %{SERVER_PORT} 80 
2

一個更普遍的方法是添加一個條件,如果該URL直接相匹配的現有文件或目錄,不能重寫:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

這樣,你不這樣做必須在您的重寫規則中維護/上傳/圖像等文件夾的列表。如果/ upload中不存在file.doc,則重寫將完成。

1

您可以比較特定羣體的像這樣的比賽:

RewriteCond $1 !=upload 
RewriteRule ^([A-Za-z]+)/([A-Za-z]+)$ index.php?dept=$1&n=$2 [QSA] 

注意!=指示逐一比較,但你可以使用正則表達式,以及:

RewriteCond $1 !^upload$ 
RewriteRule ^([A-Za-z]+)/([A-Za-z]+)$ index.php?dept=$1&n=$2 [QSA]