Apache .htaccess:ErrorDocument 和 RewriteEngine 无法协同工作

发布于 2024-08-25 06:36:17 字数 351 浏览 1 评论 0原文

请查看以下 .htaccess

ErrorDocument 404 /404/

RewriteEngine On
RewriteRule (.*) index.php [L]

通过此设置,我在 PHP 中使用 header('HTTP/1.1 404 Not Found'); 重定向到错误处理页面并发送适当的 HTTP 状态代码。发送了正确的 404 状态代码,但浏览器显示空白页面,并且访问日志显示 "GET /invalid-url/ HTTP/1.1" 404 -

谁能告诉我如何使 ErrorDocument 与Apache URL 重写?

Please take a look at the following .htaccess

ErrorDocument 404 /404/

RewriteEngine On
RewriteRule (.*) index.php [L]

With this setup, I am using header('HTTP/1.1 404 Not Found'); in PHP to redirect to the error handling page and send the appropriate HTTP status code. The correct 404 status code is sent, but the browser shows a blank page and the access log shows "GET /invalid-url/ HTTP/1.1" 404 -

Can anyone tell me how to make ErrorDocument work with Apache URL rewrites?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄凉少年不暖心 2024-09-01 06:36:17

是的,在重写规则之后声明 404 文档

这是正常的,您的服务器不会推送任何 404 错误,您正在使用包罗万象的正则表达式 ((.*)) 作为唯一的重写规则。

但你的问题与 htaccess 无关。
在 php 中,如果您发送 404 标头,浏览器不会自动重定向到 404 页面,但您必须自己在 PHP 中提供错误页面内容,因为大多数具有内部路由系统的框架都是这样做的。

Yes declare 404 document after the Rewrite rules

It's normal your server do not push any 404 error, you're using a catchall regexp ((.*)) as the only rewrite rule.

But your issue is not really htaccess related.
In php if you send 404 header the browser wont be redirected to the 404 page automaticaly, but you have to serve error page content yourself in PHP, as it is done by most frameworks with internal routing system.

梦里泪两行 2024-09-01 06:36:17

我想我的问题的答案是:我不需要。
我试图完成的是“404 重定向”。也就是说,当请求无效 URL 时,重定向到 404 文档并设置“状态 404 未找到”标头。
我不确定这是否是我想要的,因为无效的 URL 应该用 404 状态代码标记,而不是重定向。

I guess the answer to my question is: I don't need to.
What I was trying to accomplish was a "404 redirect". That is, when requesting an invalid URL, redirect to the 404 document along with setting the "Status 404 Not found" header.
And I am not sure that's something I want, because invalid URLs should be marked with the 404 status code, not redirected.

楠木可依 2024-09-01 06:36:17

如果您在 ErrorDocument 之前定义 Rewrite,它应该可以工作。

It should work if you define the Rewrite before the ErrorDocument.

柠北森屋 2024-09-01 06:36:17

试试这个


ErrorDocument 404 /404.html

RewriteEngine On
RewriteRule (.*) index.php [L]

Try this


ErrorDocument 404 /404.html

RewriteEngine On
RewriteRule (.*) index.php [L]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文