.ht 使用 mod_rewrite 访问自定义错误文档


.htaccess custom error document using mod_rewrite

我在主机上处理错误文档时遇到一些问题(GoDaddy)。

以下是我的htaccess内容:

AddHandler x-httpd-php5-4 .php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?route=$1 [L,QSA,NE]
ErrorDocument 404 /error/NotFound
ErrorDocument 403 /error/Forbidden
RedirectMatch ^/'+(.*)$ http://plus.google.com/[my_profile]

#Options All -Indexes
DirectoryIndex index.php
AddDefaultCharset UTF-8
DefaultLanguage es-PE
SetEnvIfNoCase User-Agent .*google.* search_robot
SetEnvIfNoCase User-Agent .*yahoo.* search_robot
SetEnvIfNoCase User-Agent .*bot.* search_robot
[...]
SetEnvIfNoCase User-Agent “(Ubuntu-feisty)$” bad_bot
Order Deny,Allow
Deny from env=bad_bot
#Deny from All
Allow from env=search_robot
<ifModule mod_gzip.c>
   [...]
</ifModule>
<FilesMatch ".(ttf|otf|eot|woff)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>
#COUNTRY_BLOCK_START 
<Limit GET HEAD POST>
order deny,allow
allow from [ip_range]
deny from all
</Limit>

有了所有这些,自定义错误页面不起作用。

尝试将错误文件.html、.php或任何扩展名

添加到
ErrorDocument 404 /error/NotFound(.html)
ErrorDocument 403 /error/Forbidden(.html)

或者,如果已使用 .htaccess 文件删除文件扩展名,则添加斜杠

ErrorDocument 404 /error/NotFound/
ErrorDocument 403 /error/Forbidden/

路由正在处理针对不存在的文件的所有请求(这将触发 404)。您需要做的是在路由脚本(index.php)中查找路由不存在的时间,然后从那里返回错误文档。

由于这些条件:

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

通常属于 404 错误的任何内容都将被捕获并路由到您的index.php脚本。