Apache 将域请求重写为子域请求
警告:我不是经过培训或行业培训的 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 文档和我能找到的所有 Google 提示,但我只是没有看到它。
谢谢~
Caveat: I am not an Apache expert or webmaster by training or trade (C++ developer), so I expect this is a fairly obvious, newbie-level question. Apologies in advance.
I need an Apache 2.x rewrite rule that will map a requested domain into our domain as a subdomain.
Simplified Example(s):
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
I've tried a variety of suggested options, but so far, no joy. The latest attempt is:
RewriteRule ([^.]+)\.com(.*) http://$1.masterdomain.com [L]
Note: this lives in a virtual host that owns port 80 on a specific IP, so nothing else interesting going on in the VHost that I can see having any affect on this.
I believe my problem is all in my regex, but honestly, it's eluding me.
Any assistance would be much apperciated. I've been studying the Apache docs and all the Googled tips I can find, but I'm just not seeing it.
Thanks~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,不需要重写域名,只要“源”域和“目标”域由同一台服务器处理即可。 您只需为不同的域指定别名即可引用同一主机。 具体来说:我想现在您有
块,看起来像这样:您所要做的就是添加
ServerAlias
指令,如下所示:这将让 Apache 以与处理
domain1.masterdomain.com
相同的方式处理域名domain1.com
和www.domain1.com
。Actually, there's no need to rewrite the domain name, as long as the "source" and "destination" domains are handled by the same server. You just need to alias the different domains to refer to the same host. Specifically: I suppose that right now you have
<VirtualHost>
blocks that look something like this:All you have to do is add the
ServerAlias
directives as shown below:This will make Apache handle the domain names
domain1.com
andwww.domain1.com
the same way it handlesdomain1.masterdomain.com
.感谢您的快速回复! 实际上,我有一个 VHost 来处理所有站点(因为它们都具有相同的结构),利用 VirtualDocumentRoot 指令。 如果需要,我可以发布整个 VHost 条目,但我认为这对于我的问题来说有点过分了。
自从我上一篇文章以来,我已经取得了进步。 以下内容根据需要执行重写,尽管它更新了浏览器 URL,并且我希望用户不知道他们已在内部重定向。
我不确定为什么它会在重定向时更新浏览器 URL。 我想我读到只有当您在规则行末尾显式添加 [R] 时才会发生这种情况。 最终,浏览器仍然需要显示:test1.com/index.php?id=5,而cgi内部接收test1.masterdomain.com/index.php?id=5。
另一个可能有帮助(或没有帮助)的数据点,我在 rewrite.log 中看到,在比赛之后,它隐式执行重定向(rc=302)。 这是我认为很重要的两行:
非常感谢任何想法/建议! 拥有 20 多年 C++ 经验,但使用 Apache 还不到一周,所以仍然是一个非常挣扎的新手。
-wb
附言 由于锁定的网络应用程序的一些编码要求,我实际上需要 cgi 收到的 url 为 xxx.masterdomain.com (长话短说,不是我的选择或方法;只是想解决我遇到的问题是它的要求非常有限……另外,我对经过处理的数据表示歉意——遵循高层的指示-微笑-)再次感谢!
Thanks for the fast reply! Actually, I have a single VHost that's handling all the sites (since they all have the same structure), leveraging VirtualDocumentRoot directives. I can post the whole VHost entry if desired, but I thought that would be overkill for my question.
I have made progress since my previous post. The following executes the rewrite as desired, albeit it updates the browser url, and I'm looking to NOT have the user know they've been redirected internally.
I'm not sure why it's updating the browser URL on it's redirect though. I thought I read that only happens if you explicitly add a [R] on the end of the rule line. Ultimately, the browser needs to still display: test1.com/index.php?id=5, while internally the cgi receives test1.masterdomain.com/index.php?id=5.
Another data point that may be helpful (or not), I see in my rewrite.log that, after the match, it's implicitly executing a redirect (rc=302). Here's the two lines I think are important:
Any thoughts/suggestions are greatly appreciated! 20+ years C++, but just under a week with Apache, so still a very struggling newbie.
-wb
P.S. due to some coding requirements for the web apps that are locked, I actually need the url received by the cgi to be xxx.masterdomain.com (long story, not my choice or approach; just trying to solve the problem I've been given with it's highly constrained requirements.... Also, my apologies for the sanitized data-- following directives from on-high. -smile-) Thanks again!