2013-02-14 156 views
1

我有下面的代碼,這需要網址,如「www.example.com/foo」,它改寫爲「www.example.com/html/foo.php」htaccess的 - 測試重寫URL

RewriteCond %{REQUEST_FILENAME} !-d [NC] 
RewriteRule ^([^\.]+)$ html/$1.php [NC] 

我想修改這個,如果重寫的url:'www.example.com/html/foo.php'不存在,URL會被重寫爲'www.example.com/html/fallback.php?查詢=富'

我一直在繞圈走了幾個小時。任何解決方案

回答

2

你可以嘗試做該文件存在先檢查(使用-f)然後事後做,你必須重寫:

RewriteCond %{REQUEST_FILENAME} !-d [NC] 
RewriteCond %{REQUEST_FILENAME} !-f [NC] 
RewriteCond %{REQUEST_URI} ^([^\.]+)$ 
RewriteCond %{DOCUMENT_ROOT}/html/%1.php !-f 
RewriteRule^html/fallback.php?query=%1 [L,QSA] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^\.]+)$ html/$1.php [L] 
+0

完美的作品 - 非常感謝! – hunter 2013-02-14 23:31:20