Reputation: 4265
First, I am not great at writing regular expressions, so I apologize for that.
I am trying to setup IIS to rewrite URLs from one main entry to a secondary blue / green entry site. As part of a multi-tenancy setup users are redirected by US state abbreviations. For now, we will only be looking for the 2 letters after the domain name. All traffic will be coming in to the main entry site https://dev.app.com
and redirected to the appreciate state's URL
For example:
http://dev.app.com/ca
or http://dev.app.com/ny
By default, users that come into the root of the site http://dev.app.com
will be automatically redirected to http://dev.app.com/ca
. CA is our default state.
Each state has its own blue / green entry site. For example: http://ca.app.entry
. The blue / green entry sites have a reverse proxy rewrite rule to redirect traffic to whatever site is currently running. The blue / green rewrite rule is updated during deployment.
Also, each state has its own API. For example: http://ca.api.com
. The main entry URL would look like this: https://dev.app.com/ca/ext
and it would forward traffic to the correct state's API.
I have been trying to setup the rewrite rules to trap the state abbreviation and also look for the ext
if it exists and forward to the appropriate AI site.
For the default URL I started out by creating a redirect rule that comes before the rewrite rules
<httpRedirect enabled="true">
<add wildcard="/" destination="/ca" />
</httpRedirect>
The next rule I am trying to trap the state abbreviation:
<rule name="StateRewrite" stopProcessing="true">
<match url="/([a-z]{2})/$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="http://{R:1}.app.entry" logRewrittenUrl="true" />
</rule>
Testing the expression I get the proper 2 letter state abbreviation as long as there is nothing after the [state]/. So http://dev.app.com/ca/someOtherqueryString does not produce a match. Going to http://dev.app.com/ca/ does not seem to forward traffic to [state].app.entry/[state]. If I go directly to the state's entry site it works. So, going to ca.app.entry/ca routes to the proper blue / green site. Looking at the logs it doesn't seem to be constructing the the entire URL. I am only seeing the /[state]/
in the log: 2018-12-27 16:57:25 127.0.0.1 GET /ca/
For the API site I tried a pattern rule like this (.*)/ext/(.*)
. The rule comes after the state abbreviation rule, but it is still not routing to the proper state API site.
I could use some help constructing the rules and how they need to be ordered.
Upvotes: 0
Views: 219
Reputation: 445
the following regex will return 2 chars from the end of the string [a-z]{2}$
if there can be a / after that then it would be like this [a-z]{2}/?$
that is for regex as for the rewrite rules it should work but i am not sure exactly how the rewrites work with substitutions so hope this helps
Upvotes: 1