简体   繁体   English

如何在 IIS7 上设置 URL Rewrite 以便多个子域重定向到正确的网站?

[英]How to set up URL Rewrite on IIS7 so that multiple subdomains redirect to the correct web site?

I asked a previous question about how to have multiple subdomains all pointing at the same site;我问了一个关于如何让多个子域都指向同一个站点的问题; the answer I accepted there was to use URL Rewrite.我接受的答案是使用 URL Rewrite。

Cool.凉爽的。 But for the life of me I cannot figure out how URL Rewrite works, and I consider myself a relatively smart guy.但是对于我来说,我无法弄清楚 URL Rewrite 是如何工作的,我认为自己是一个相对聪明的人。 |-) Lots of questions... |-) 很多问题...

  • Each customer (and there will be hundreds, if not thousands) gets their own subdomain eg customer1.mydomain.com , cooldude.mydomain.com etc. The regex would be (.+)\.mydomain\.com , and all of these URLs should be redirected to a website on IIS that I've named customers.mydomain.com .每个客户(如果不是数千个,也会有数百个)都有自己的子域,例如customer1.mydomain.comcooldude.mydomain.com等。正则表达式为(.+)\.mydomain\.com ,所有这些URL 应该重定向到 IIS 上我命名为customers.mydomain.com的网站。 All the examples I've found on URL Rewrite are about referencing documents, eg mydomain.com/thing.aspx?id=123 changes to mydomain.com/thing/123 , which I'm not really interested in. Here's a clue: as you can see in the picture below, the "Input" column always says "URL path after '/'" - but there doesn't appear to be any way to change that.我在 URL Rewrite 上找到的所有示例都是关于引用文档的,例如mydomain.com/thing.aspx?id=123mydomain.com/thing/123的更改,我对此并不真正感兴趣。这是一个线索:正如您在下图中看到的,“输入”列总是显示“'/' 之后的 URL 路径”——但似乎没有任何方法可以改变它。

URL 重写图片

  • I am assuming that the rewrite rule should be put on the default web site, but I want the rule to redirect to the customers.mydomain.com web site.我假设重写规则应该放在默认网站上,但我希望规则重定向到customers.mydomain.com网站。 How do you force the redirect to a specific web site, in such a way that I will still be able to see the subdomain name (which determines the customer site I'm logging into)?你如何强制重定向到一个特定的网站,这样我仍然可以看到子域名(它决定了我登录的客户网站)?

I think what you want to do to get this to work is add an input condition to your rewrite rule.我想你想要做的就是为你的重写规则添加一个输入条件。 You can read about it in the "Referencing a rewrite map from rewrite rule" section at http://learn.iis.net/page.aspx/469/using-rewrite-maps-in-url-rewrite-module/ .您可以在http://learn.iis.net/page.aspx/469/using-rewrite-maps-in-url-rewrite-module/的“从重写规则引用重写映射”部分阅读相关内容。

Here's an example I think will work for you:这是一个我认为对你有用的例子:

<rule name="My Rule">
    <match url="(.+)?" negate="false" />
    <action type="Redirect" url="http://{C:1}.mydomain.com/{R:0}" />
    <conditions>
    <add input="{HTTP_HOST}" pattern="(.+)\.mydomain\.com" />
    </conditions>
</rule>

I added an answer to your original question, which in turn answers this one too.我为您的原始问题添加了一个答案,这反过来也回答了这个问题。 I think for what you're trying to do, reading the host header is easier than doing redirects.我认为对于您要尝试做的事情,读取主机标头比进行重定向更容易。

How to create subdomains for IIS7 programmatically? 如何以编程方式为 IIS7 创建子域?

The user interface isn't very good, but this can be done using only the IIS URL Rewrite module GUI installed via Microsoft Web Platform Installer as per these instructions .用户界面不是很好,但这可以仅使用 IIS URL 重写模块 GUI 完成,该 GUI 是按照这些说明通过 Microsoft Web 平台安装程序安装的。 As far as I can tell there isn't any way to change the value in the Input column from the value URL path after '/' , but you can still choose the target destination ( Rewrite URL ) based on the entire URL including the subdomain.据我所知,没有任何方法可以从URL path after '/'更改输入列中的值,但您仍然可以根据包括子域在内的整个 URL 选择目标目的地(重写 URL ) .

When you create or edit a rule, the Action you take should be set to Rewrite .当您创建或编辑规则时,您采取的操作应设置为Rewrite The Action Properties' Rewrite URL field (the destination address) can include built-in server variables from IIS (in this case insufficient), and regex capture groups from Conditions that you add. Action Properties 的Rewrite URL字段(目标地址)可以包括来自 IIS 的内置服务器变量(在这种情况下是不够的),以及来自您添加的 Conditions 的正则表达式捕获组。 While you might not want a condition to filter anything out, you can create a sufficiently generic condition that also serves to create a capture group value to reference in the Rewrite URL field.虽然您可能不希望条件过滤掉任何内容,但您可以创建一个足够通用的条件,该条件还用于创建捕获组值以在重写 URL字段中引用。

In your case, edit (or create) the Rewrite rule in the GUI, expand the Conditions area, and add a new condition based on the SERVER_NAME .在您的情况下,在 GUI 中编辑(或创建)重写规则,展开条件区域,并添加基于SERVER_NAME的新条件。 In your case, you could enter:在您的情况下,您可以输入:

Condition input: {SERVER_NAME}
Check if input string: Matches the Pattern
Pattern: ^([-A-z0-9]+)\.(.*)$

You might need to modify the first part of the pattern if you need additional characters to be included.如果您需要包含其他字符,您可能需要修改模式的第一部分。 It matches a subdomain from an input like www.google.com .它匹配来自www.google.com等输入的子域。 Make sure this regex is generic enough that it doesn't filter out anything you want to be forwarded, since Conditions are intended to act as filters.确保此正则表达式足够通用,不会过滤掉您要转发的任何内容,因为条件旨在充当过滤器。

If you click on "Test Pattern", you can see if you have created a valid regex, and also see what the name of the capture group is, which you will want to use later in your Rewrite URL field.如果单击“测试模式”,您可以查看是否创建了有效的正则表达式,还可以查看捕获组的名称,稍后您将在重写 URL 字段中使用它。 Since I didn't create any other Conditions, for my regex, the subdomain will have the value {C:1} .由于我没有创建任何其他条件,对于我的正则表达式,子域将具有值{C:1}

Create the condition, and go down to the Action Properties Rewrite URL field.创建条件,然后转到 Action Properties Rewrite URL 字段。 In your case, you can enter在你的情况下,你可以输入

http://example.com/{C:1}

This should function right away, without having to edit any of the XML configuration of the default website by hand.这应该会立即起作用,而无需手动编辑默认网站的任何 XML 配置。

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

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