Apache 的自定义 404 错误问题 - ErrorDocument 也是 404
我正在尝试为我的网站创建自定义 404 错误。我正在 Windows 上使用 XAMPP 对此进行测试。
我的目录结构如下:
error\404page.html
index.php
.htaccess
我的 .htaccess 文件的内容是:
ErrorDocument 404 error\404page.html
这会产生以下结果:
然而,这不起作用 - 这是否与斜杠的方式有关,或者我应该如何引用错误文档?
站点站点文档驻留在网络根目录的子文件夹中,这对我应该如何引用有什么影响吗?
当我将文件更改为时,
ErrorDocument 404 /error/404page.html
我收到以下错误消息,该消息不是我链接的 html 文件内的内容 - 但它与上面列出的内容不同:
I am trying to create a custom 404 error for my website. I am testing this out using XAMPP on Windows.
My directory structure is as follows:
error\404page.html
index.php
.htaccess
The content of my .htaccess file is:
ErrorDocument 404 error\404page.html
This produces the following result:
However this is not working - is it something to do with the way the slashes are or how I should be referencing the error document?
site site documents reside in a in a sub folder of the web root if that makes any difference to how I should reference?
When I change the file to be
ErrorDocument 404 /error/404page.html
I receive the following error message which isn't what is inside the html file I have linked - but it is different to what is listed above:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ErrorDocument
指令(如果提供)本地 URL 路径,期望该路径完全符合文档根
。在您的情况下,这意味着ErrorDocument
的实际路径是当您在第二次尝试中更正它时,您看到该页面的原因是因为
http://localhost/error/ 404page.html
不存在,因此在查找错误处理文档时出现 404 错误。The
ErrorDocument
directive, when supplied a local URL path, expects the path to be fully qualified from theDocumentRoot
. In your case, this means that the actual path to theErrorDocument
isWhen you corrected it in your second try, the reason you see that page instead is because
http://localhost/error/404page.html
doesn't exist, hence the bit about there being a 404 error in locating the error handling document.由于性能问题,目前 Apache 中默认禁用 .htaccess 文件。启用 .htaccess 文件后,Web 服务器必须在其所在的每个子目录中的每次点击时检查该文件。
只是认为值得注意这一点很重要。如果您无论如何都想打开 .htaccess 文件,这里有一个解释它的链接:
http: //www.tildemark.com/enable-htaccess-on-apache/
.htaccess files are disabled by default in Apache these days, due to performance issues. When .htaccess files are enabled, the web server must check for it on every hit in every subdirectory from where it resides.
Just figured it was important to note. If you want to turn on .htaccess files anyway, here's a link that explains it:
http://www.tildemark.com/enable-htaccess-on-apache/
您可以将它们添加到任何虚拟主机文件中,而不是将它们添加到 .htaccess 文件中。
其中
error-pages
是mywebsite
文件夹中的子文件夹,包含自定义错误页面。确保重新启动 apache 以查看更改。Instead of adding them to your .htaccess file, you can add them to any of your virtual host files.
Where
error-pages
is a subfolder in themywebsite
folder, containing the custom error pages. Make sure you restart your apache to view your changes.无论您输入的默认 404 页面是什么,都必须是绝对的或相对于根文件夹的:这就是为什么有些人会错误地将其 url 视为重写引擎的 url,而重写引擎的 url 是相对于 .htaccess 所在文件夹的。
Whatever url you enter as the default 404 page must be either absolute or relative from the root folder : That's why some make the mistake of treating its url like the rewrite engines' url which is relative from the folder where .htaccess is located.