自定义错误403页PHP


Custom Error 403 Page PHP

我在不希望文件被直接访问的目录中创建了一个.htaccess。它工作并触发Apache服务器的默认403页面(禁止访问!)。如何创建自定义403页面?谢谢!

在您的.htaccess文件中,您可以指定您想要的文档作为默认的403错误文档。

ErrorDocument 403 /dir/file.html

此处的目录相对于文档根目录

您可以这样做:


#Rewrite URL's
RewriteEngine On
RewriteRule ^404/?$ errors/404.html [NC]
# Enable Error Documents
# (404,File Not Found) | (403,Forbidden) | (500,Internal Server Error)
ErrorDocument 404 /404
ErrorDocument 403 /404

这样做的是打开重写引擎,这样我们就可以很好地重定向url,然后我们使用重写规则定义/404/或/404应该重定向到自定义的404页面。然后我声明ErrorDocument 404和403应该重定向到404页面。我这样做是为了安全,所以用户不知道文件是否存在,或者他们只是没有访问权限。

你也可以在你的。htaccess文件中通过ErrorDocumnet添加一个自定义的内联错误消息,如下所示:

    ErrorDocument 403 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>403 Forbidden</title></head><p>Sorry, access to this page is forbidden.</p></body></html>'

都不适合我。我必须编辑文件/etc/apache2/conf-enabled/localized-error-pages.conf并添加以下

ErrorDocument 403 "403, Ok!?"