IIS7.5 URL Rewrite Regex matching when it shouldn't
-
16-04-2021 - |
Question
I have split some pages in between subdomains and want to do a URL rewrite to different pages on different subdomains in certain cases. Everything is a rewrite rule except for the final two rules in the file. Those last two rules determine which subdomain to route the path I fixed to.
The way I am doing it is if I prepend the path with an underscore (_) then it stays on subdomain A. If I prepend the path with a tilde (~) then it is redirected to subdomain B.
So I have this rule:
<rule name="Login rule" stopProcessing="false">
<match url="(.*?)/?old-path/Login\.aspx$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_METHOD}" pattern="GET" />
</conditions>
<action type="Rewrite" url="~new-path/login.aspx" />
</rule>
Please notice there is an aspx on the end of the URL. It continues processing, but I have a generic rewrite rule at the end of the list right before the redirect ones. This is to remove all ASPX extensions on subdomain A (www), but I want to leave the ASPX extension for subdomain B (Please don't suggest removing the suggested on the 2nd subdomain. Thanks :)
<rule name="Remove ASPX" stopProcessing="false">
<match url="^([^www\.]+)\.aspx$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="_{R:1}" />
</rule>
The problem is, is this won't work because all the URLs have www in the beginning. I am not that good with regex, but I am guessing I need to just apply this rule to all URL that has a tilde in it. I tried this, but it's not really working either:
<match url="^_+\.aspx$" />
Basically I want this rule to ignore URLs that I have rewritten to have a ~
in them, but remove the ASPX if I placed the _
at the start of the path.
Any suggestions?
Solution
If I'm understood your problem then you have URL: "~new-path/login.aspx" and you want do redirect to "~new-path/login", right?
Then your regex should be like this:
^(.*~.*)\.aspx$
Note: "www" is a part of domain name and not included into matching.
So if your full URL is "http://www.mysite.com/~new-path/login.aspx" then only "~new-path/login.aspx" piece will take part in regex matching.
And template {R:1} will contain value in first group (braces): "~new-path/login"