使用 htaccess 将整个站点重定向到其他域

发布于 2024-10-07 02:16:17 字数 331 浏览 0 评论 0原文

我想将整个网站从一个域重定向到另一个域。 当我声明时我就工作 RewriteRule 的 R=301 但用户可以很容易注意到 他被重定向到导航栏中的其他网址。 我想要实现的结果是使用远程服务器来显示内容 url 保留在本地服务器上。

例如: 用户输入 example1.com,htaccess 从 example2.com 获取内容 (对于 example1.com 中的主页和每个子页面)。服务器没有 重定向他的浏览器,但只加载来自 example1.com 的远程数据。

当我只使用 file_get_contentes('example2.com... 但是 如果有一些 $_POST 数据,就会出现问题。

I want to redirect entire site from one domain to other.
I works when i declare
RewriteRule with R=301 but user can easy notice that
he is redirected to other url in his navi bar.
The result i want to achieve is using remote server to display content
with url keeped from local server.

For example:
User types example1.com, the htaccess gets content from example2.com
(for main page and every subpage in example1.com). Server does not
redirect his browser but just loads remote data from example1.com.

It works when i just use file_get_contentes('example2.com... but
problem comes if there is some $_POST data.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情域 2024-10-14 02:16:17

干净的解决方案是让处理 example2.com 的服务器响应发送到 example1.com 的请求。这通常是通过将 Apache 虚拟主机配置为使用 ServerName example2.comServerAlias example1.com 来完成的。

如果您负担不起这样做,您应该依赖实际的代理和反向代理,而不是使用 PHP 自己完成:它更快,并且可以正确处理绝对 URL(如果 example2.com 返回包含 ?) 的 HTML。 Apache 有正确的代理和反向代理实现。

The clean solution is to have the server that handles example2.com respond to requests sent to example1.com. This is usually done by configuring an Apache virtual host to use ServerName example2.com and ServerAlias example1.com.

If you cannot afford to do this, you should rely on an actual proxy-and-reverse-proxy instead of doing it yourself with PHP: it's faster, and it handles absolute URLs correctly (what happens if example2.com returns HTML that contains an <a href="http://example2.com/xxx"> ?). Apache has a correct proxy and reverse proxy implementation.

情丝乱 2024-10-14 02:16:17

不是您问题的答案,但由于多种原因,此解决方案并不是最佳方案:

  1. 它取决于两台服务器的启动情况。
  2. 由于必须发出两次请求,因此速度较慢。
  3. 它会占用双倍的带宽,并且如果您使用 PHP 脚本,还会占用代理服务器上的额外资源。

您确定必须这样做吗?

如果您确实这样做,请研究 Apache 的代理功能而不是 PHP 脚本可以处理您描述的问题(以及其他几个问题,例如会话管理,以及通过 HTTP_REFERERHTTP_USER_AGENT 变量传递)。 这里是一个教程。

请注意,这需要对服务器进行 root 访问。

Not an answer to your question, but this solution is not optimal for a number of reasons:

  1. It depends on two servers being up.
  2. It is slower as the request has to be made twice.
  3. It takes up double the bandwidth, and if you use a PHP script, additional resources on the proxying server.

Are you sure you have to do this?

If you really do, look into Apache's proxying capabilities instead of a PHP script that can deal with the problem you describe (and several others, for example session management, and passing through the HTTP_REFERER and HTTP_USER_AGENT variables). Here is a tutorial for it.

Note that this requires root access to the server.

请持续率性 2024-10-14 02:16:17

我不相信 .htaccess 可以做到这一点。
您是否无法在 httpd.conf 中添加 example1.com 作为 example2.com 的别名,并更改 example1.com 的 DNS 记录以指向与 example2.com 相同的计算机?

这将实现您所追求的目标并消除中间人(example1.com 的服务器)。

I don't believe this is possible with .htaccess.
Could you not add example1.com as an alias in the httpd.conf for example2.com and change the DNS records for example1.com to point to the same machine as example2.com ?

That'll achieve what you are after and cuts out the middleman (example1.com's server).

醉殇 2024-10-14 02:16:17

您无法仅使用 mod_rewrite 进行此类跨域重定向。写入另一台服务器上的地址的重写规则将始终表现得好像它具有 R 标志。

您可能必须使用一些服务器端脚本,但不确定如何使用。

You can't do such a redirect across domains using only mod_rewrite. A rewriterule that writes to an address on another server will always behave as if it has an R flag.

You'll probably have to use some server-side scripting, not sure how though.

长发绾君心 2024-10-14 02:16:17

我通过使用解决了这个问题:

$opts = array('http' =>
  array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => (isset($_POST)) ? http_build_query($_POST) : '',
  )
);

$context = stream_context_create($opts);
$result  = file_get_contents('http://example2.com/'.$_SERVER['REQUEST_URI'], false, $context);

我意识到这是一个相当蹩脚的解决方案,但效果很好。

I solved it by using:

$opts = array('http' =>
  array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => (isset($_POST)) ? http_build_query($_POST) : '',
  )
);

$context = stream_context_create($opts);
$result  = file_get_contents('http://example2.com/'.$_SERVER['REQUEST_URI'], false, $context);

I realise that is quite lame solution but it works well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文