可以模仿跨域 mod-rewrite 规则吗?

Can one mimic a cross-domain mod-rewrite rule?

如果 domain1.com 和 domain2.com 驻留在不同的主机上,是否可以模仿重写规则,使 //domain1.com/anypagename.php?key=value 重定向到 //domain1.com/anypagename.php?key=value?常规 mod-rewrite 不允许这样做,但也许重写规则的组合使所有内容都指向同一个文件并让该文件转发它,可以模仿这个吗?

我之所以要这样做是因为domain1是一个大域的受信任的子域,所以我们想保留它用于营销目的,但我们无法更改DNS记录并且关联的托管服务是太糟糕了,我们希望实际网站 运行 在我们自己的服务器上(并且 domain2.com 是我们控制下的常规域)。

使用 .htaccess、PHP 等方法都很好,但重要的是 GET 和 POST 请求任意页面(使用任意 ?key=val&key2=val2 参数) domain1 在 domain2 上被正确处理并显示为好像它们实际上是 domain1 上的常规页面。

我没试过,但我想到了 P|proxy 标志

For example, if you wanted all image requests to be handled by a back-end image server, you might do something like the following:
...

在您的情况下,当 domain1.comdomain2.com 设置相同时,这可能是

RewriteRule ^ http://domain2.com%{REQUEST_URI} [P]

不过,还要注意

Performance warning

Using this flag triggers the use of mod_proxy, without handling of persistent connections. This means the performance of your proxy will be better if you set it up with ProxyPass or ProxyPassMatch


所以 ProxyPass 可能是更好的选择

ProxyPass / http://domain2.com

但是这个也有一个缺点,你不能在 .htaccess 文件中使用它。这仅在您有权访问主或虚拟主机配置时才有效。