如何使用errordocument重定向到同一文件夹中的php文件?

发布于 2024-08-10 18:18:45 字数 486 浏览 3 评论 0原文

我想接受特定文件夹中丢失文件的请求,并将它们重定向到该文件夹​​中的program.php

这是一个我会在不同地方经常使用的程序,我希望避免在 htaccess 文件中指定文件夹的名称。

我尝试过只是输入:

errordocument 404 program.php

但结果是浏览器只是打印出“program.php”并且不执行program.php。

我理解为什么会发生这种情况:基于 的文档http://httpd.apache.org/docs/2.0/mod/core.html,不以斜杠或 http 开头的 url 将被解释为文本消息。

问题是如何绕过此限制以便能够访问同一文件夹。

I want to take requests for missing files in a specific folder and redirect them to program.php in that folder.

This is a program I will use a lot in different places, and I would prefer to avoid specifying the name of the folder in the htaccess file.

I have tried just putting:

errordocument 404 program.php

but the result is that the browser just prints out "program.php" and doesn't execute program.php.

I understand why this is happening: based on the documentation at http://httpd.apache.org/docs/2.0/mod/core.html, url's that don't begin with a slash or http are interpreted as a text message.

The question is how to get around this limitation to be able to access the same folder.

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

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

发布评论

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

评论(6

赠我空喜 2024-08-17 18:18:45

我最终使用 rewriterule 而不是 errordocument

rewriteengine on
rewritecond %{request_filename} !-f
rewriterule ^(.+).jpg$ program.php?i=$1.jpg [L]

第二行验证该文件不存在,以便正确显示现有图像。

I ended up using rewriterule instead of errordocument:

rewriteengine on
rewritecond %{request_filename} !-f
rewriterule ^(.+).jpg$ program.php?i=$1.jpg [L]

The second line verifies that the file does not exist, so that existing images will be shown correctly.

蔚蓝源自深海 2024-08-17 18:18:45

您想要:

ErrorDocument 404 /program.php

根据 docs,“URL 可以开始本地 Web 路径(相对于 DocumentRoot)带有斜杠 (/)。”

You want:

ErrorDocument 404 /program.php

According to the docs, "URLs can begin with a slash (/) for local web-paths (relative to the DocumentRoot)."

无风消散 2024-08-17 18:18:45

您是否尝试过(相对或绝对)路径?
如果您将其设置为相对的,则它应该与网络根目录相关,这可能会满足您的目的。

ErrorDocument 404 /program.php

Have you tried a (relative or absolute) path?
If you make it relative, it should be to the web root, which might server your purpose.

ErrorDocument 404 /program.php
笑,眼淚并存 2024-08-17 18:18:45

如果您只有少量文件夹,您可能可以使用:

<Directory /path/a>
   ErrorDocument 404 /a/program.php
</Directory>
<Directory /path/b>
   ErrorDocument 404 /b/program.php
</Directory> 
<Directory /path/c>
   ErrorDocument 404 /c/program.php
</Directory>

如果这是不切实际的,那么您应该在根目录中只有一个program.php,并让它根据 REDIRECT_URL 环境变量($ )的内容做出不同的响应_SERVER['REDIRECT_URL'] 在 php.ini 中

If you have only a small number of folders, you can probably use:

<Directory /path/a>
   ErrorDocument 404 /a/program.php
</Directory>
<Directory /path/b>
   ErrorDocument 404 /b/program.php
</Directory> 
<Directory /path/c>
   ErrorDocument 404 /c/program.php
</Directory>

If that is impractical, then you should only have one program.php in the root and have it respond differently depending on the contents of the REDIRECT_URL environment variable, which is $_SERVER['REDIRECT_URL'] in php.

梨涡少年 2024-08-17 18:18:45

为什么不让一个 ErrorDocument 处理所有文件夹中的所有错误?这样可能会干净得多。然后,您可以使用 $_SERVER["REQUEST_URI"] 处理个别情况,如果我没记错的话,它应该包含原始请求。

Why not have one ErrorDocument handle all the errors in all the folders? Might be much cleaner that way. You could then handle the individual case using $_SERVER["REQUEST_URI"] which should contain the original request if I remember correctly.

混浊又暗下来 2024-08-17 18:18:45

这个怎么样。在根路径中,您设置了一个通用的 error.php 文件。然而,它所做的只是解析 REQUEST_URI,检查路径,查看那里是否有自定义的 error.php,并将其包含在内。可以工作吧?

How about this. In your root path, you set up a generic error.php file. However, all it does is parse the REQUEST_URI, check the path, see whether there is a custom error.php there, and include that. Could work, huh?

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