限制对错误页面的直接访问

时间:2011-12-12 01:37:03

标签: .htaccess mod-rewrite

我正在使用.htaccess显示错误的自定义HTML页面(401,404,500 ...等)。我想限制直接访问包含html页面的errors文件夹。在errors文件夹的.htaccess文件中,我有以下内容:

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com
RewriteRule \.(html|htm)$ - [F] 

这似乎没有用,我得到了:

a 500 Internal Server Error error was encountered while trying to use an 
ErrorDocument to handle the request.

我必须对.htaccess文件进行哪些更改才能使其正常工作。我也想知道如果我把error文件夹放在public_html文件夹之外它是否会起作用?

2 个答案:

答案 0 :(得分:5)

这是你应该放在你的.htaccess(在你的public_html中)

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^(www\.)?mydomain\.com
RewriteRule \.(html|htm)$ - [F]

让我知道它是否有效

答案 1 :(得分:0)

在.htaccess文件中,将所有错误重定向到一个php文件

ErrorDocument 403 /errors.php
ErrorDocument 404 /errors.php
ErrorDocument 500 /errors.php
error.php中的

使用带有if else语句的$ _SERVER ['REDIRECT_STATUS']来检测错误并显示自定义消息

if($_SERVER['REDIRECT_STATUS'] == 403)
{
    die("Forbidden");
}
else if($_SERVER['REDIRECT_STATUS'] == 404)
{
    die("NOT FOUND");
}
else if($_SERVER['REDIRECT_STATUS'] == 500)
{
    die("Server Error");
}
else if($_SERVER['REDIRECT_STATUS'] == 200)
{
 // user is trying to directly access the errors page, redirect to index.php
 header("Location: index.php");
}
else
{
    // all other error codes
}

这是List of HTTP status codes。为要检测的每个错误编写else if语句。未检测到的错误将落入else块