如何使用errordocument重定向到同一文件夹中的php文件?
我想接受特定文件夹中丢失文件的请求,并将它们重定向到该文件夹中的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我最终使用 rewriterule 而不是 errordocument:
第二行验证该文件不存在,以便正确显示现有图像。
I ended up using rewriterule instead of errordocument:
The second line verifies that the file does not exist, so that existing images will be shown correctly.
您想要:
根据 docs,“URL 可以开始本地 Web 路径(相对于 DocumentRoot)带有斜杠 (/)。”
You want:
According to the docs, "URLs can begin with a slash (/) for local web-paths (relative to the DocumentRoot)."
您是否尝试过(相对或绝对)路径?
如果您将其设置为相对的,则它应该与网络根目录相关,这可能会满足您的目的。
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.
如果您只有少量文件夹,您可能可以使用:
如果这是不切实际的,那么您应该在根目录中只有一个program.php,并让它根据 REDIRECT_URL 环境变量($ )的内容做出不同的响应_SERVER['REDIRECT_URL'] 在 php.ini 中
If you have only a small number of folders, you can probably use:
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.
为什么不让一个 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.
这个怎么样。在根路径中,您设置了一个通用的 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?