在文档root之外执行PHP脚本?

发布于 2025-02-03 01:55:15 字数 2797 浏览 4 评论 0原文

我有以下目录结构:

/htdocs
     ├──test
     │     └──.htaccess
     └──myphp.php

/htdocshttps://example.com/htdocs/test 的根是http://test.example.com 我的目标是执行myphp.php脚本并将fileName作为参数传递,当client requests ./ images/* ie https://example.com/test/ images/blah.pnghttp://test.example.com/images/blah.png

为此,我正在使用mod-rewrite apache2 。

RewriteEngine On 
RewriteRule ^(images/.*)$ ../myphp.php?$1 [L]

​错误400(不良请求),带有http://test.example.com/images/blah.png

[2022-05-28 14:36:10] [authz_core:debug] [pid 26880:tid 1888] mod_authz_core.c(815): [client 127.0.0.1:9862] AH01626: authorization result of Require all granted: granted
[2022-05-28 14:36:10] [authz_core:debug] [pid 26880:tid 1888] mod_authz_core.c(815): [client 127.0.0.1:9862] AH01626: authorization result of <RequireAny>: granted
[2022-05-28 14:36:10] [core:error] [pid 26880:tid 1888] [client 127.0.0.1:9862] AH00126: Invalid URI in request 'myfile.php' 'GET /images/blah.png HTTP/1.1'
[2022-05-28 14:36:10] [authz_core:debug] [pid 26880:tid 1896] mod_authz_core.c(815): [client 127.0.0.1:9863] AH01626: authorization result of Require all granted: granted, referer http://test.example.com/images/blah.png
[2022-05-28 14:36:10] [authz_core:debug] [pid 26880:tid 1896] mod_authz_core.c(815): [client 127.0.0.1:9863] AH01626: authorization result of <RequireAny>: granted, referer http://test.example.com/images/blah.png

如果我尝试.htaccess中的绝对路径,

RewriteRule ^(images/.*)$ /htdocs/myphp.php?$1 [L]

我会得到错误403(禁止):

[2022-05-28 14:39:02] [authz_core:debug] [pid 26880:tid 1908] mod_authz_core.c(815): [client 127.0.0.1:2752] AH01626: authorization result of Require all granted: granted
[2022-05-28 14:39:02] [authz_core:debug] [pid 26880:tid 1908] mod_authz_core.c(815): [client 127.0.0.1:2752] AH01626: authorization result of <RequireAny>: granted
[2022-05-28 14:39:02] [core:error] [pid 26880:tid 1908] (20023)The given path was above the root path: [client 127.0.0.1:2752] AH00127: Cannot map GET /images/blah.png HTTP/1.1 to file
[2022-05-28 14:39:02] [authz_core:debug] [pid 26880:tid 1908] mod_authz_core.c(815): [client 127.0.0.1:2752] AH01626: authorization result of Require all granted: granted, referer http://test.example.com/images/blah.png
[2022-05-28 14:39:02] [authz_core:debug] [pid 26880:tid 1908] mod_authz_core.c(815): [client 127.0.0.1:2752] AH01626: authorization result of <RequireAny>: granted, referer http://test.example.com/images/blah.png

如果没有硬编码域名的情况,如何在可能的情况下进行此工作?

I have the following directory structure:

/htdocs
     ├──test
     │     └──.htaccess
     └──myphp.php

/htdocs is root for https://example.com and /htdocs/test is root for http://test.example.com
My goal is to execute myphp.php script and pass filename as argument when client requests ./images/* i.e. https://example.com/test/images/blah.png or http://test.example.com/images/blah.png

For this I'm using mod-rewrite in Apache2.4, I have the following in .htaccess:

RewriteEngine On 
RewriteRule ^(images/.*)$ ../myphp.php?$1 [L]

This works great with http://example.com/test/images/blah.png, however I'm getting error 400 (bad request) with http://test.example.com/images/blah.png

[2022-05-28 14:36:10] [authz_core:debug] [pid 26880:tid 1888] mod_authz_core.c(815): [client 127.0.0.1:9862] AH01626: authorization result of Require all granted: granted
[2022-05-28 14:36:10] [authz_core:debug] [pid 26880:tid 1888] mod_authz_core.c(815): [client 127.0.0.1:9862] AH01626: authorization result of <RequireAny>: granted
[2022-05-28 14:36:10] [core:error] [pid 26880:tid 1888] [client 127.0.0.1:9862] AH00126: Invalid URI in request 'myfile.php' 'GET /images/blah.png HTTP/1.1'
[2022-05-28 14:36:10] [authz_core:debug] [pid 26880:tid 1896] mod_authz_core.c(815): [client 127.0.0.1:9863] AH01626: authorization result of Require all granted: granted, referer http://test.example.com/images/blah.png
[2022-05-28 14:36:10] [authz_core:debug] [pid 26880:tid 1896] mod_authz_core.c(815): [client 127.0.0.1:9863] AH01626: authorization result of <RequireAny>: granted, referer http://test.example.com/images/blah.png

If I try absolute path in .htaccess:

RewriteRule ^(images/.*)$ /htdocs/myphp.php?$1 [L]

I get error 403 (forbidden):

[2022-05-28 14:39:02] [authz_core:debug] [pid 26880:tid 1908] mod_authz_core.c(815): [client 127.0.0.1:2752] AH01626: authorization result of Require all granted: granted
[2022-05-28 14:39:02] [authz_core:debug] [pid 26880:tid 1908] mod_authz_core.c(815): [client 127.0.0.1:2752] AH01626: authorization result of <RequireAny>: granted
[2022-05-28 14:39:02] [core:error] [pid 26880:tid 1908] (20023)The given path was above the root path: [client 127.0.0.1:2752] AH00127: Cannot map GET /images/blah.png HTTP/1.1 to file
[2022-05-28 14:39:02] [authz_core:debug] [pid 26880:tid 1908] mod_authz_core.c(815): [client 127.0.0.1:2752] AH01626: authorization result of Require all granted: granted, referer http://test.example.com/images/blah.png
[2022-05-28 14:39:02] [authz_core:debug] [pid 26880:tid 1908] mod_authz_core.c(815): [client 127.0.0.1:2752] AH01626: authorization result of <RequireAny>: granted, referer http://test.example.com/images/blah.png

Any suggestions how to make this work if possible without hard-coding domain names?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文