0

警告:我不是Apache 专家或受过培训或交易的网站管理员(C++ 开发人员),所以我认为这是一个相当明显的新手级问题。提前道歉。

我需要一个 Apache 2.x 重写规则,它将请求的域作为子域映射到我们的域中。

简化示例:

domain1.com/index.php?option=80     ->      domain1.masterdomain.com/index.php?option=80
www.domain1.com/index.php?option=99 ->      domain1.masterdomain.com/index.php?option=99
domain2.com/index.php?option=33     ->      domain2.masterdomain.com/index.php?option=33
www.domain2.com/index.php?option=44 ->      domain2.masterdomain.com/index.php?option=44

我尝试了各种建议的选项,但到目前为止,没有任何乐趣。最近的尝试是:

RewriteRule   ([^.]+)\.com(.*) http://$1.masterdomain.com [L]

注意:这存在于一个虚拟主机中,该主机在特定 IP 上拥有端口 80,因此我可以看到 VHost 中没有其他有趣的事情对此有任何影响。

我相信我的问题都在我的正则表达式中,但老实说,它在逃避我。

任何援助都会非常感激。我一直在研究 Apache 文档和我能找到的所有谷歌提示,但我只是没有看到它。

谢谢~

4

2 回答 2

3

其实不需要重写域名,只要“源”和“目的”域由同一台服务器处理即可。您只需为不同的域设置别名即可引用同一主机。具体来说:我想现在你的<VirtualHost>块看起来像这样:

<VirtualHost *:80>
    ServerName domain1.masterdomain.com
    # other rules
</VirtualHost>
<VirtualHost *:80>
    ServerName domain2.masterdomain.com
    # other rules
</VirtualHost>

您所要做的就是添加ServerAlias如下所示的指令:

<VirtualHost *:80>
    ServerName domain1.masterdomain.com
    ServerAlias domain1.com
    ServerAlias www.domain1.com
    # same other rules as before
</VirtualHost>
<VirtualHost *:80>
    ServerName domain2.masterdomain.com
    ServerAlias domain2.com
    ServerAlias www.domain2.com
    # same other rules as before
</VirtualHost>

domain1.com这将使 Apache 以相同的www.domain1.com方式处理域名domain1.masterdomain.com

于 2009-06-06T04:01:18.457 回答
0

感谢您的快速回复!实际上,我有一个 VHost 处理所有站点(因为它们都具有相同的结构),利用 VirtualDocumentRoot 指令。如果需要,我可以发布整个 VHost 条目,但我认为这对我的问题来说太过分了。

自从我上一篇文章以来,我已经取得了进展。以下内容根据需要执行重写,尽管它更新了浏览器 url,并且我希望不让用户知道他们已在内部重定向。

 RewriteCond    %{HTTP_HOST}    ^(www\.)?([^.]+)\.com$ [NC]
 RewriteRule    ^(.*)$      http://%2.masterdomain.com$1

我不确定为什么它会在重定向时更新浏览器 URL。我以为我读到只有在规则行末尾明确添加 [R] 时才会发生这种情况。最终,浏览器仍需要显示:test1.com/index.php?id=5,而 cgi 在内部接收 test1.masterdomain.com/index.php?id=5。

另一个可能有用(或没有帮助)的数据点,我在我的 rewrite.log 中看到,在匹配之后,它隐式执行重定向(rc=302)。这是我认为重要的两行:

 implicitly forcing redirect (rc=302) with http://test1.masterdomain.com/index.php
 redirect to http://test1.masterdomain.com/index.php?id=5 [REDIRECT/302]

非常感谢任何想法/建议!20 多年的 C++,但使用 Apache 不到一周,所以仍然是一个非常挣扎的新手。

-wb

PS由于对被锁定的网络应用程序的一些编码要求,我实际上需要cgi收到的url是xxx.masterdomain.com(长话短说,不是我的选择或方法;只是想解决我一直存在的问题考虑到它的高度受限的要求....另外,我对经过清理的数据表示歉意——遵循来自最高层的指示。-smile-) 再次感谢!

于 2009-06-06T06:54:19.713 回答