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

发布于 2024-07-22 12:07:42 字数 888 浏览 4 评论 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 文档和我能找到的所有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

颜漓半夏 2024-07-29 12:07:42

实际上,不需要重写域名,只要“源”域和“目标”域由同一台服务器处理即可。 您只需为不同的域指定别名即可引用同一主机。 具体来说:我想现在您有 块,看起来像这样:

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

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

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

梦纸 2024-07-29 12:07:42

感谢您的快速回复! 实际上,我有一个 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 (长话短说,不是我的选择或方法;只是想解决我遇到的问题是它的要求非常有限……另外,我对经过处理的数据表示歉意——遵循高层的指示-微笑-)再次感谢!

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.

 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.

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:

 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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文