Apache重写域请求子域请求

时间:2022-03-28 11:04:29

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.

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

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~

2 个解决方案

#1


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 *: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.masterdomain.com相同的方式处理域名domain1.com和www.domain1.com。

#2


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.

谢谢你的快速回复!实际上,我有一个处理所有站点的VHost(因为它们都具有相同的结构),利用VirtualDocumentRoot指令。如果需要,我可以发布整个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. 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.

我不确定为什么它会更新浏览器URL的重定向。我以为我读过只有在规则行的末尾明确添加[R]才会发生。最终,浏览器仍然需要显示: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). Here's the two lines I think are important:

另一个可能有用(或没有)的数据点,我在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]

Any thoughts/suggestions are greatly appreciated! 20+ years C++, but just under a week with Apache, so still a very struggling newbie.

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

-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!

附:由于对锁定的网络应用程序的一些编码要求,我实际上需要cgi收到的网址为xxx.masterdomain.com(长篇故事,不是我的选择或方法;只是试图解决我已经给出的问题随着它的高度约束的要求....此外,我对消毒数据道歉 - 遵循从高开始的指令.- smile-)再次感谢!

#1


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 *: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.masterdomain.com相同的方式处理域名domain1.com和www.domain1.com。

#2


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.

谢谢你的快速回复!实际上,我有一个处理所有站点的VHost(因为它们都具有相同的结构),利用VirtualDocumentRoot指令。如果需要,我可以发布整个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. 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.

我不确定为什么它会更新浏览器URL的重定向。我以为我读过只有在规则行的末尾明确添加[R]才会发生。最终,浏览器仍然需要显示: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). Here's the two lines I think are important:

另一个可能有用(或没有)的数据点,我在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]

Any thoughts/suggestions are greatly appreciated! 20+ years C++, but just under a week with Apache, so still a very struggling newbie.

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

-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!

附:由于对锁定的网络应用程序的一些编码要求,我实际上需要cgi收到的网址为xxx.masterdomain.com(长篇故事,不是我的选择或方法;只是试图解决我已经给出的问题随着它的高度约束的要求....此外,我对消毒数据道歉 - 遵循从高开始的指令.- smile-)再次感谢!