2010-10-30 63 views
0

我奮力實現這個簡單的事情......重寫規則混淆

我有一些靜態頁面應該像
www.domain.com/profile等。怎麼寫 問題是爲了重寫規則..

會有一些固定的重寫 例如/ home

我想存在不重寫每一個文件 www.domain.com/test.php應該去 測試.php

最後,如果沒有找到它,我希望它被重定向到static.php?_.....

RewriteRule ^/home/?$ /index.php?__i18n_language=$1 [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^/([^/]+)/?$ /static.php?__i18n_language=$1 

這工作正常,但如果我的index.php或test.php的,甚至鍵入從其他重定向馬赫它讓我在static.php ... 請幫助!

回答

1

根據你的描述,你可以使用這些規則:

# stop rewriting process if request can be mapped onto existing file 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteRule^- [L] 

# rewrite known paths /home, /foo, /bar, etc. 
RewriteRule ^/(home|foo|bar|…)$ /index.php [L] 

# rewrite any other path 
RewriteRule^/static.php [L] 
+0

感謝..問題是,REQUEST_FILENAME didnt返回完整路徑!修正它與添加前綴文檔根變量 – GorillaApe 2010-10-30 13:45:19

+0

@Parhs:哦,是的,這是可能的:「完整的本地文件系統路徑的文件或腳本匹配的請求,如果這已在服務器已確定的時間'REQUEST_FILENAME'是引用。否則,例如在虛擬主機上下文中使用時,其值與「REQUEST_URI」相同(請參見['RewriteCond'指令](http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond )) – Gumbo 2010-10-30 13:49:43

1

我很久沒有使用過這個功能,但是這是我發現的,應該有所幫助。它是一箇舊腳本的一部分,只有當文檔未找到時,才生成.httaccess文件以從/usr/share/doc重定向: 規則是「檢查,如果目標網址存在,則離開「:

RewriteEngine On 
RewriteBase /doc/User_Documents 

### If the directory already exists, then leave 
### We're just redirecting request when the directory exists but has been renamed. 

RewriteCond %{REQUEST_FILENAME} User_Documents/([^/]+-[0-9][^/]*) 
RewriteCond $PWD/%1 -d 
RewriteRule .* - [L] 

這是[L],這意味着離開,如果條件之一是匹配的。在舊腳本中,有數百個生成的規則(在[L]之後)被跳過,因爲其中一個條件匹配。在你的情況下,當找到目標%{REQUEST_FILENAME}時,你會跳過其餘的規則。

所以,我建議,重定向規則之前:

RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule .* - [L]