自定义错误 403 页面 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8703540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom Error 403 Page PHP
提问by fart-y-goer
I created a .htaccess inside a directory in which I don't want the files to be directly accessed. It works and fires the default 403 page (Access forbidden!) of the Apache server. How can I create a custom 403 page? Thanks!
我在不希望直接访问文件的目录中创建了一个 .htaccess。它工作并触发 Apache 服务器的默认 403 页面(禁止访问!)。如何创建自定义 403 页面?谢谢!
回答by JK.
In your .htaccess
file you can specify what document you want as your default 403 error document.
在您的.htaccess
文件中,您可以指定您想要作为默认 403 错误文档的文档。
ErrorDocument 403 /dir/file.html
Here the directory is relative to the document root.
这里的目录是相对于文档根目录的。
回答by Aramael Pena-Alcantara
You can do something like the following:
您可以执行以下操作:
#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
What this is doing is turning on the RewriteEngine so we can redirect url's nicely, then we are defining using the RewriteRule that /404/ or /404 should redirect to the custom 404 page. I then state that the ErrorDocument 404 and 403 should redirect to the 404 page. I do this for security so, a user does not know whether or not a file exists or if they just don't have access.
这样做是打开 RewriteEngine 以便我们可以很好地重定向 url,然后我们使用 RewriteRule 定义 /404/ 或 /404 应该重定向到自定义 404 页面。然后我声明 ErrorDocument 404 和 403 应该重定向到 404 页面。我这样做是为了安全,因此,用户不知道文件是否存在,或者他们是否无权访问。