2012-02-21 45 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