【问题标题】:Why is rewriteRule in httpd.conf not working on this Apache proxy server?为什么 httpd.conf 中的 rewriteRule 在这个 Apache 代理服务器上不起作用?
【发布时间】:2020-10-26 17:01:22
【问题描述】:

我知道这个问题与本网站上的其他问题相似。我花了几天时间阅读这些帖子并尝试不同的方法。没有一个有效。

我正在尝试使用 Apache 代理服务器将特定 URL 映射到新 URL。我在本地网络上有一个专用设备(黑匣子),它可以调用 original.remoteHost1.com。当 Black Box 请求服务时,我希望使用此本地网络上的 Apache 代理服务器来替换 new.remotehost2.com 为 original.remoteHost1.com。我可以将 Black Box 配置为指向代理服务器,但 Black Box 没有公开一个接口来更改其 HTTP 服务请求的目标主机。

Black Box 只提出两个非常具体的请求:

  1. http://original.remoteHost1.com/rectrack/file_name.php?data_1=text_string_1

  2. http://original.remoteHost1.com/rectrack/file_name.php?data_1=text_string_1&data_2=text_string_2

我想改成:

  1. http://new.remotehost2.com/rectrack/file_name.php?data_1=text_string_1

  2. http://new.remotehost2.com/rectrack/file_name.php?data_1=text_string_1&data_2=text_string_2

作为代理,Apache 服务器接受黑盒请求并将它们转发到 original.remoteHost1.com。我希望我能够使用 httpd.conf 中的 rewriteRule 将 original.remoteHost1.com 更改为 new.remotehost.com,但我无法让这个看似简单的功能起作用。

我已尝试使用以下 rewriteRule(以及许多变体)将 new.remotehost.com 替换为 original.host.com,但我似乎无法让 Apache 实际进行替换:

LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine on
RewriteCond %{HTTP_HOST} original.remoteHost1.com
RewriteRule   ^(.*)  http://new.remotehost2.com/$1 [P]

有没有人知道为什么新的 URL 没有代替原来的 URL?

【问题讨论】:

    标签: mod-rewrite proxy


    【解决方案1】:

    我能够解决这个问题。我不记得我为什么要尝试这个,我当然也不知道它为什么会起作用。我正在运行 Windows 7。在文件夹 C:\Windows\System32\Drivers\etc\ 中有一个名为“hosts”的文件。在该文件中,在与 localhost 名称解析相关的部分中,我添加了以下行:

    127.0.0.1   original.remoteHost1.com
    

    完成此操作后,rewriteCond 和 rewriteRule 的行为与我预期的一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-10
      • 2021-03-29
      • 2016-12-21
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多