在文档root之外执行PHP脚本?
我有以下目录结构:
/htdocs
├──test
│ └──.htaccess
└──myphp.php
/htdocs
是https://example.com
和/htdocs/test 的根是
我的目标是执行http://test.example.com
myphp.php
脚本并将fileName作为参数传递,当client requests ./ images/*
ie https://example.com/test/ images/blah.png
或http://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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论