.htaccess htaccess可以从另一个域返回输出而不发出301吗?

ej83mcc0  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(115)

假设我有abc.com和def.com。在def.com上,我有一个显示“hello”的def.com/test.html
我希望这样做,如果我执行GET abc.com/something/,它将直接返回def.com/test.html的内容200而不是301 def.com/test.html。我不希望用户知道两台服务器之间发生了通信。
现在,我可以设置一个abc.com/.htaccess到url重写为abc.com/test.php,这将负责回显其他网站的内容与CURL如下:

<?
$url = "def.com/test.html"; //Would be received from the .htaccess's URL rewrite, say $_GET['wantedURL']
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

但是,是否有一种方法可以告诉.htaccess自己执行CURL(或者返回执行此操作的shell脚本的输出),如下所示?

RewriteEngine on
RewriteRule ^/something/$ <curl def.com/test.html> [L]
#NOT THIS: RewriteRule ^/something/$ def.com/test.html [R=301,L]

当然,我将对给定模式的几个动态URL进行此操作,因此我只对路径进行了硬编码,以解释我所要查找的内容
谢谢!
---编辑---
我发现Can ProxyPass and ProxyPassReverse work in htaccess?似乎需要把它放在vhosts配置中,这是我想避免的。我更喜欢通过. htaccess来完成。在.htaccess中尝试上面的结果确实是他们解释为不可能的:ProxyPassReverse not allowed here
---编辑2 ---
根据@MrWhite的建议,我现在得到了No protocol handler was valid for the URL /something/. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule
但我认为一切都是加载的。当我做httpd -M | grep "proxy"时,我得到:

proxy_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared)
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_scgi_module (shared)
proxy_wstunnel_module (shared)

这是我的博客:

<VirtualHost *:80>
    ServerName abc.com
    ProxyPass "/" "https://def.com/"
    ProxyPassReverse "/" "https://def.com/"
</VirtualHost>
p1iqtdky

p1iqtdky1#

.htaccess中,您可以在mod_rewrite RewriteRule指令上使用Pproxy)标志,通过mod_proxy发送请求。
例如,在abc.com处的.htaccess文件中:

RewriteEngine on
RewriteRule ^something/$ https://def.com/test.html [P]

(Note,当在.htaccess中使用时,RewriteRule * 模式 * 匹配的URL路径不以斜杠开头。)
但是,这自然要求mod_proxy * 和 * 相关模块已经在服务器配置中加载/启用。
如果你没有在服务器配置中设置ProxyPassReverse(正如你所说的,这在.htaccess中是不允许的),那么目标域(即def.com)发出一个重定向到自己并有效地 * 中断 * 反向代理将是微不足道的。尽管,如果目标域在你的控制之下,这不一定是一个问题。

相关问题