简体   繁体   English

Apache将域请求重写为子域请求

[英]Apache rewrite domain requests to subdomain requests

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. 警告:我不是经过培训或交易(C ++开发人员)的Apache专家或网站管理员,所以我希望这是一个相当明显的新手级别的问题。 Apologies in advance. 提前致歉。

I need an Apache 2.x rewrite rule that will map a requested domain into our domain as a subdomain. 我需要一个Apache 2.x重写规则,该规则将请求的域作为子域映射到我们的域中。

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. 注意:这位于一个虚拟主机中,该主机在特定IP上拥有端口80,因此在VHost中没有其他有趣的事情发生,我可以看到对此有任何影响。

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. 我一直在研究Apache文档和我能找到的所有Googled技巧,但我只是没有看到它。

Thanks~ 谢谢〜

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: 具体来说:我想现在您有<VirtualHost>块,看起来像这样:

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

All you have to do is add the ServerAlias directives as shown below: 您所要做的就是添加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>

This will make Apache handle the domain names domain1.com and www.domain1.com the same way it handles domain1.masterdomain.com . 这将使Apache处理domain1.comwww.domain1.com的方式与处理domain1.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. 实际上,我只有一个VHost可以利用VirtualDocumentRoot指令来处理所有站点(因为它们都具有相同的结构)。 I can post the whole VHost entry if desired, but I thought that would be overkill for my question. 如果需要,我可以发布整个VHost条目,但是我认为这对我的问题来说是过大了。

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. 以下内容将根据需要执行重写,尽管它会更新浏览器URL,但我希望用户不要知道它们已在内部重定向。

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

I'm not sure why it's updating the browser URL on it's redirect though. 我不确定为什么要在重定向时更新浏览器URL。 I thought I read that only happens if you explicitly add a [R] on the end of the rule line. 我以为我读到的情况只有在您在规则行的末尾显式添加[R]时才会发生。 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. 最终,浏览器仍然需要显示:test1.com/index.php?id=5,而内部cgi接收到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). 另一个可能有用(或无效)的数据点,我在rewrite.log中看到,在匹配之后,它隐式地执行了重定向(rc = 302)。 Here's the two lines I think are important: 我认为以下两行很重要:

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

Any thoughts/suggestions are greatly appreciated! 任何想法/建议都将不胜感激! 20+ years C++, but just under a week with Apache, so still a very struggling newbie. C ++已有20多年的历史,但使用Apache不到一周,因此仍然是一个非常苦苦挣扎的新手。

-wb -wb

PS 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! PS由于对锁定的Web应用程序有一些编码要求,因此我实际上需要cgi接收的url为xxx.masterdomain.com(长话短说, 不是我的选择或方法;只是试图解决我一直遇到的问题同样,我对清理过的数据表示歉意-遵从高位的指示-smile-)再次感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM