2012-02-21 76 views
1

我正在嘗試編寫一個.htaccess文件來引導併發送帶有錯誤代碼的索引。示例index.php?error404或index.php?error501。使用.htaccess捕獲錯誤

我可以使用下面的代碼在根目錄中指示錯誤。

ErrorDocument 404 index.php?error=404 
ErrorDocument 501 index.php?error=501 

例子http://www.domain.com/file_does_not_exist.php這個工作。 結果http://www.domain.com/index.php?error=404

問題是從子目錄捕獲併發送到index.php錯誤。

示例http://www.domain.com/directory_does_not_existhttp://www.domain.com/directory_and/file_does_not_exist.php

結果是http://www.domain.com/directory_does_not_exist/index.php?error=404因此index.php的CSS中斷了。

我嘗試了下面的代碼沒有成功。我正在嘗試通過重寫URL來重定向到index.php。

RewriteCond $1 !^err[45][0-9][0-9]$ 
RewriteRule ^\/.\/\.php$ /index.php?s=$1 [R] 

AND 

RewriteCond $1 !^err[45][0-9][0-9]$ 
RewriteRule ^([/]?*)\.php$ /index.php?s=$1 [QSA,L] 

任何幫助,將不勝感激。

回答

3

無需使用重寫;只使用絕對URL在ErrorDocument指令(用/開始他們):

ErrorDocument 404 /index.php?error=404 
ErrorDocument 501 /index.php?error=501 

The docs表演絕對URL的例子:

URL可以與本地web的斜槓(/)開始路徑(相對於DocumentRoot),或者是客戶端可以解析的完整URL。或者,可以提供消息以由瀏覽器顯示。示例::

ErrorDocument 500 http://foo.example.com/cgi-bin/tester 
ErrorDocument 404 /cgi-bin/bad_urls.pl 
ErrorDocument 401 /subscription_info.html 
ErrorDocument 403 "Sorry can't allow you access today" 
+0

快速正確! – undone 2012-02-21 03:45:43