当目录不存在但由 RewriteRule 创建时,为什么错误文档 404 没有捕获?

发布于 2024-09-26 00:37:06 字数 694 浏览 1 评论 0原文

我的 htaccess 中有这个:

Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteRule ^annons/([a-zA-Z0-9_]+)$ ad.php?ad_id=$1 [NC]
ErrorDocument 404 /404.html

除了一个问题之外,这一切都有效。

当我写一个不存在的地址时,如下所示:

   http://www.domain.com/some_adress_that_doesnt_exist

然后显示 404.html。

但是,如果我这样写:(

   http://www.domain.com/annons/some_adress_that_doesnt_exist

注意 /annons/ 实际上并不存在;它是在重写规则中“创建”的。)

只有当我写 /annons/then 一个不存在的 url 表明 404 不起作用。它适用于所有其他子目录和组合,但我猜我的重写规则有一些缺陷......

我所说的不工作是指显示一个空白的白色页面,其中没有信息,就好像该页面存在但完全空白。

I have this in my htaccess:

Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteRule ^annons/([a-zA-Z0-9_]+)$ ad.php?ad_id=$1 [NC]
ErrorDocument 404 /404.html

This all works, except for one problem.

When I write an address that doesn't exist, like this:

   http://www.domain.com/some_adress_that_doesnt_exist

then the 404.html is displayed.

BUT, if I write it like this:

   http://www.domain.com/annons/some_adress_that_doesnt_exist

(Note the /annons/ doesn't actually exist; it is "created" in the rewrite-rules.)

It is ONLY when I write the /annons/ and then a url that doesn't exist that the 404 doesn't work. It works with all other subdirectories and combinations, but I am guessing my rewriterule has some flaws...

By not working I mean a blank white page shows up with no information in it, as if the page existed but was completely blank.

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

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

发布评论

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

评论(2

很快妥协 2024-10-03 00:37:06

好吧,由于此类 URL 被重写为实际存在的文件(即 ad.php),因此找到了所请求的文件。

现在,如果您的 ad.php 脚本确定所请求的资源不存在,您需要使用 PHP 来处理该问题,方法是响应 404 状态代码,例如:

header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');

之后您可以将错误文档返回到得到相同的结果:

readfile('404.html');

Well, since such URLs are getting rewritten to an actually existing file (i.e. ad.php), the requested file was found.

Now if your ad.php script decides that the requested resource does not exist, you need to handle that with PHP by responding with a 404 status code, e.g.:

header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');

After that you then could return the error document to get the same result:

readfile('404.html');
我是有多爱你 2024-10-03 00:37:06

只有当我写了 /annons/ 然后写了一个不存在的 url 时,404 才不起作用。

服务器如何知道哪些 /annons/ 存在,哪些不存在?您总是重定向到ad.php,而确实存在。就服务器而言,这里不会发生 404。

您需要在脚本中处理不存在广告的情况,例如抛出 HTTP/1.0 404 Not Found 标头并显示错误页面。 (或者,更好的是,使用 SERVER_PROTOCOL 变量,如 @Gumbo 所示)

It is ONLY when I write the /annons/ and then a url that doesn't exist that the 404 doesn't work.

How is the server supposed to know which /annons/ exist and which ones don't? You are always redirecting to ad.php which does exist. No 404 happens here as far as the server is concerned.

The case of a non-existant ad is something you will need to deal with inside your script, e.g. by throwing a HTTP/1.0 404 Not Found header and showing an error page. (or, better, use the SERVER_PROTOCOL variable as @Gumbo demonstrates)

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