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.
Cool. But for the life of me I cannot figure out how URL Rewrite works, and I consider myself a relatively smart guy. |-) Lots of questions...
- Each customer (and there will be hundreds, if not thousands) gets their own subdomain e.g.
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 namedcustomers.mydomain.com
. All the examples I've found on URL Rewrite are about referencing documents, e.g.mydomain.com/thing.aspx?id=123
changes tomydomain.com/thing/123
, which I'm not really interested in. Here's a clue: a开发者_Go百科s 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.
- 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. 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/.
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?
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. 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.
When you create or edit a rule, the Action you take should be set to 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. 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.
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. 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
. 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. Since I didn't create any other Conditions, for my regex, the subdomain will have the value {C:1}
.
Create the condition, and go down to the Action Properties Rewrite URL field. 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.
精彩评论