【问题标题】:Apache rewrite domain requests to subdomain requestsApache 将域请求重写为子域请求
【发布时间】:2010-10-31 19:54:24
【问题描述】:

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

我需要一个 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 文档和我能找到的所有 Google 提示,但我只是没有看到它。

谢谢~

【问题讨论】:

  • 这实际上可能更像是一个ServerFault类型的问题......

标签: apache subdomain rewrite


【解决方案1】:

其实不需要重写域名,只要“源”和“目的”域由同一台服务器处理即可。您只需为不同的域设置别名即可引用同一主机。具体来说:我想现在你有<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>

这将使 Apache 处理域名 domain1.comwww.domain1.com 的方式与处理 domain1.masterdomain.com 的方式相同。

【讨论】:

  • 好主意!这意味着大多数时候实际上不需要正则表达式重定向。
  • 是的,您真正需要 mod_rewrite 的情况非常少见。或者至少,人们倾向于过度使用它。
【解决方案2】:

感谢您的快速回复!实际上,我有一个 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

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

【讨论】:

    猜你喜欢
    • 2015-07-26
    • 2013-01-17
    • 1970-01-01
    • 2012-04-09
    • 2021-09-11
    • 2021-12-30
    • 1970-01-01
    • 2016-09-20
    • 2015-08-09
    相关资源
    最近更新 更多