IIS rewrite rules skipped even though the pattern matches - iis

Sorry for the vague title, the problem is too complex to summarize in a short phrase...
I'm trying to set up the following redirection rules:
blog.mydomain.net/en/something: redirected to www.mydomain.com/something
blog.mydomain.net/fr/something: redirected to www.mydomain.fr/something
blog.mydomain.net/*: redirected to www.mydomain.com
Rule 3 is working, but rules 1 and 2 seem to be skipped so rule 3 is always applied. Here are my web.config rules:
<!-- Canonicalize mydomain.com to www.mydomain.com -->
<rule name="CanonicalHostNameRule_en" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain\.com$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:1}" />
</rule>
<!-- Canonicalize mydomain.fr to www.mydomain.fr -->
<rule name="CanonicalHostNameRule_fr" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain\.fr$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.fr/{R:1}" />
</rule>
<!-- Redirect blog.mydomain.net/en/something to www.mydomain.com/something -->
<rule name="RedirectBlog_en" enabled="true" stopProcessing="true">
<match url="^/en(/.*)?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:1}" />
</rule>
<!-- Redirect blog.mydomain.net/fr/something to www.mydomain.fr/something -->
<rule name="RedirectBlog_fr" enabled="true" stopProcessing="true">
<match url="^/fr(/.*)?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.fr/{R:1}" />
</rule>
<!-- Redirect blog.mydomain.net/* to www.mydomain.com -->
<rule name="RedirectBlog_other" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/" />
</rule>
<!-- Wordpress-specific rules -->
...
I don't understand why rules RedirectBlog_en and RedirectBlog_fr are skipped; I tested the regular expressions and they work fine.
Can anyone spot the problem?
EDIT: if I disable the 3rd rule (RedirectBlog_other), then rules 1 and 2 work fine... how is it possible, since rules 1 and 2 are executed before rule 3?

OK, I got it!
First, things weren't happening as I thought; rules 1 and 2 were not working when I disabled rule 3: I was still redirected to my actual domain, but this was done by Wordpress, not by my rules.
Second, my pattern for matching the URL was wrong: the leading '/' is not included in the input, so my rules weren't matching at all.

Related

IIS 7 Redirect to new domain

Good morning,
I am trying to create a Rewrite rule in IIS to redirect a specific page to a new url however when I apply the rule no redirect happens what am I missing? Code:
</rule>
<rule name="Redirect" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^(/)?$" />
<add input="{HTTP_HOST}" pattern="domain1/server/sdk/" />
</conditions>
<action type="Redirect" url="https://domain2/rest/" />
</rule>
Try adding REQUEST_URI to the pattern match
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/" />
match url may also work better as
<match url="^(.*)$" />
also make sure the rule is enabled, your version has enabled="false" on it
Here is my full rule
<rule name="domain1/server/sdk/" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/" />
</conditions>
<action type="Redirect" url="https://domain2/rest/" redirectType="Permanent" appendQueryString="false" />
</rule>
Excellent thank you very much Mike, I did disable my rule after testing it and it did not work which is why it shows enabled=false however I revised the rule per your advice and that worked for me! Incase this is helpful for anyone else in the future my full rule is:
<rules>
<remove name="Portal Redirect" />
<rule name="(rule name)" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Redirect" url="Domain2/rest/" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/rest/" />
</conditions>
</rule>

URL Rewrite-rule in IIS 8 not triggering

I have a problem with my URL rewrite and I don't know what I'm doing wrong so, maybe you can point me in the right direction. We had a intranet-site, which had a pattern like this: intranet.old-site.com. Now we got a new domain and I want to forward my outdated links to the front-page of our new intranet, which looks like: intranet.new-site.com.
I installed the URL Rewrite module in IIS and in my point of view, the setup is correct:
Match URL
Request URL: matches pattern.
Using: Wildcards
Pattern: *intranet.old-site*
Ignore case: true
Conditions
none
Server Variables
none
Action
Action Type: Rewrite
Action Properties: Rewrite-URL: http://intranet.new-site.com/
Append query string: true
Log rewritten URL: false
Stop processing of subsequent rules: false
I'm also open for any idea what might work, so if there is a rule for the web.config, I could try that as well.
Update 1:
the web-config looks like this now:
<rewrite>
<rules>
<rule name="RewriteSG2DE" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*).sgbdd.saint-gobain(.*)" />
<action type="Redirect" url="intranet.stark-deutschland.net" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="(.*)sgbdd.saint-gobain(.*)" />
<add input="{HTTP_HOST}" pattern="intranet.sgbdd.saint-gobain.com" />
</conditions>
</rule>
</rules>
</rewrite>
I want any site, which is basically anything like intranet.sgbdd.saint-gobain.com/start.asp?something_more" to be forwarded / redirected to intranet.stark-deutschland.net/start.asp?something_more
For instance:
intranet.sgbdd.saint-gobain.com/start.asp?something_more --> intranet.stark-deutschland.net/start.asp?something_more
somesite.intranet.saint-gobain.com/my/new/site --> somesite.intranet.stark-deutschland.net/my/new/site
I also added conditions as suggested:
Thanks for you input
Using two URL rewrite rules is much easier to handle this.
If you want to redirect
intranet.sgbdd.saint-gobain.com/start.asp?something_more --> intranet.stark-deutschland.net/start.asp?something_more
somesite.intranet.saint-gobain.com/my/new/site --> somesite.intranet.stark-deutschland.net/my/new/site
Then rule can be like this.
<rule name="rule1" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(.*)saint-gobain.com$" />
<add input="{HTTP_HOST}" pattern="intranet.sgbdd.saint-gobain.com" negate="true" />
</conditions>
<action type="Redirect" url="http://{C:1}stark-deutschland.net/{R:1}" redirectType="Temporary" />
</rule>
<rule name="rule2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^intranet.sgbdd.saint-gobain.com$" />
</conditions>
<action type="Redirect" url="http://intranet.stark-deutschland.net/{R:1}" redirectType="Temporary" />
</rule>
I tried to combine two rules to one rule but there's something wrong with expression ?! so it is recommended to split into two rules.
If you means
intranet.sgbdd.saint-gobain.com/start.asp?something_more --> intranet.stark-deutschland.net/start.asp?something_more
somesite.intranet.sgbdd.saint-gobain.com/my/new/site --> somesite.intranet.stark-deutschland.net/my/new/site
Then the rule can be like this.
<rule name="RewriteSG2DE" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://{C:1}stark-deutschland.net/{R:1}" redirectType="Temporary" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="(.*)sgbdd.saint-gobain.com" />
</conditions>
</rule>

URL rewrite multiple excludes

I'm trying to write a IIS Url rewrite rule that redirects all requests except two, and I can't figure it out.
What I'm trying to accomplish:
http://server/healthcheck.aspx --> not redirected
http://server/idsrv2/2/stuff --> not redirected
http://server/stuffstuff --> redirect to http://server/idsrv2/stuffstuff
This is the rule I have so far, but it's not kicking in:
<rule name="Redirect everything to idsrv/2" patternSyntax="Wildcard" stopProcessing="true">
<match url="^$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="^(.*)healthcheck" negate="true"/>
<add input="{REQUEST_URI}" pattern="^(.*)idsrv2" negate="true" />
</conditions>
<action type="Redirect" url="idsrv2{R:1}" appendQueryString="true"/>
</rule>
Any help appreciated!
Logical grouping in your rule should be MatchAll. I've changed you rule a bit and this should work in your case:
<rule name="Redirect everything to idsrv/2" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^/healthcheck" negate="true"/>
<add input="{REQUEST_URI}" pattern="^/idsrv2" negate="true" />
</conditions>
<action type="Redirect" url="idsrv2/{R:0}" appendQueryString="true" />
</rule>

Rewriting subdomains to subfolders and parameter in IIS

I'm trying to wrap my head around this problem;
I have a default site with an application that resides in a subfolder, e.g. /app/app.html
It also accepts a language parameter, so for example http://192.168.0.1/app/app.html?language=fi would work.
Now I have two subdomains that I need to not only get rewritten to the correct folder, but also include the language parameter. For example:
fi.domain.com -> http://1.1.1.1/app/app.html?language=fi
swe.domain.com -> http://1.1.1.1/app/app.html?language=swe
I've made A records for both subdomains to point to 1.1.1.1
Currently there are no special bindings (only port 80, no hostnames and all IPs) and no special default pages.
EDIT: I've tried using the URL rewriter module, but I haven't been able to get it work as intended.
EDIT 2: My first example of how I need it to work was a bit flawed, here's a better version;
finnishword.domain.com -> http://1.1.1.1/app/app.html?language=fi
otherwordinswedish.domain.com -> http://1.1.1.1/app/app.html?language=swe
I'm not sure to well understand your question.
It seems that you need URL rewriting rules.
According to this link
<rule name="CName to URL - Rewrite" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.domain\.com$" />
</conditions>
<action type="Rewrite" url="/app/app.html?language={C:1}" />
</rule>
I think it's what you need ;)
Edit 24/08/2016
If you can't have a dynmique pattern with RegEx you have to do as many rules as you have subdomains:
<rule name="finnishRule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(finnishword)\.domain\.com$" />
</conditions>
<action type="Rewrite" url="/app/app.html?language=fi" />
</rule>
<rule name="finnishRule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(otherwordinswedish)\.domain\.com$" />
</conditions>
<action type="Rewrite" url="/app/app.html?language=swe" />
</rule>
or you can mixup all together if you want to redirect all subdomains that contains the word "swedish" on url with ?
<rule name="finnishRule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*swedish.*)\.domain\.com$" />
</conditions>
<action type="Rewrite" url="/app/app.html?language=swe" />
</rule>
So after all the RegEx pattern is up to you ;)
Edit 25/08/2016
Maybe you have to add condition to skip static files
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
...
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<rules>
<clear />
<rule name="swedishMainRule" enabled="true" stopProcessing="true">
<match url="^$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?swedishword\.domain\.fi$" />
<add input="{QUERY_STRING}" pattern="language=" negate="true" />
</conditions>
<action type="Redirect" url="?language=sve" appendQueryString="false" logRewrittenUrl="false" />
</rule>
<rule name="swedishRule" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?swedishword\.domain\.fi$" />
</conditions>
<action type="Rewrite" url="/app/{R:1}" />
</rule>
<rule name="finnishRule" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?anotherfinnishword\.domain\.fi$" />
</conditions>
<action type="Rewrite" url="/app/{R:1}" />
</rule>
</rules>
This is how I ended up doing it, basically the first rule deal with the language parameter, and the other rules just rewrite the URL. The Finnish rule doesn't need an extra language rule since the default language of the app is Finnish.

Azure Websites Canonical HostName Rule - Loop Redirect

I have 2 custom domains set up on Azure Websites:
mydomain.lt and www.mydomain.lt
DNS registrar's settings:
Subdomain and redirect:
awverif -> CNAME: awverify.mydomain.azurewebsites.net
www -> CNAME: mydomain.azurewebsites.net
Other redirects:
IP: 137.x.x.x (IP address provided by Azure) and MX: 79.x.x.x
I can reach my website via mydomain.lt and www.mydomain.lt
What I am trying to do now is to set my Canonical URL in a way, that users coming from
www.mydomain.lt would be automatically redirected to mydomain.lt and all relative URL paths returned in a lower case.
I added settings bellow to system.WebServer on Web.config:
<rewrite>
<rules>
<!-- SEO | Section 1 | Whitelist -->
<rule name="Whitelist - Resources" stopProcessing="true">
<match url="^(?:css/|scripts/|bundles/|images/|install/|config/|umbraco/|umbraco_client/|base/|webresource\.axd|scriptresource\.axd|__browserLink|[^/]*/arterySignalR/.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
<!-- SEO | Section 2 | Rewrites (chaining) -->
<rule name="SEO - Lower case" stopProcessing="false">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_METHOD}" pattern="GET" />
<add input="{R:1}" pattern="[A-Z]" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="_{ToLower:{R:1}}" />
</rule>
<!-- SEO | Section 3 | Redirecting -->
<rule name="SEO - HTTP canonical redirect" stopProcessing="true">
<match url="^(_*)(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^www\.(.*)" />
<add input="{HTTP_METHOD}" pattern="GET" />
<add input="{SERVER_PORT}" pattern="80" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:2}" />
</rule>
<rule name="SEO - Non-canonical redirect" stopProcessing="true">
<match url="^(_+)(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_METHOD}" pattern="GET" />
</conditions>
<action type="Redirect" url="{R:2}" />
</rule>
</rules>
</rewrite>
The resulting behaviour is a redirect loop. Any ideas how to fix this problem?
It seems the problem is with Azure Websites. The link below gives a workaround how to fix the problem temporarily.
http://social.msdn.microsoft.com/Forums/wpapps/en-US/ee3a5f97-8a58-4b42-a2d9-a73cd5d12c01/issues-with-redirect-rules-used-in-url-rewrite-feature?forum=windowsazurewebsitespreview
I've rewritten my rules and everything works just fine:
<rewrite>
<rules>
<rule name="Whitelist - Resources" stopProcessing="true">
<match url="^(?:css/|scripts/|bundles/|content/|images/|install/|config/|umbraco/|umbraco_client/|base/|webresource\.axd|scriptresource\.axd|__browserLink|[^/]*/arterySignalR/.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
<rule name="Convert to lower case" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
<rule name="Canonical Host Name" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll">
<add input="{REMOTE_PORT}" pattern="*" />
<add input="{HTTP_HOST}" pattern="mydomain.lt" negate="true" />
</conditions>
<action type="Redirect" url="http://mydomain.lt/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Start by simplifying your rewrite rules to narrow down the problem:
Instead of using rewrite for www, just update the CNAME for www.domain.com to point to domain.com
remove section 3 till you get section 2 working
then try updating section 2 to:
<!-- SEO | Section 2 | Rewrites (chaining) -->
<rule name="SEO - Lower case" stopProcessing="false">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_METHOD}" pattern="GET" />
<add input="{R:1}" pattern="[A-Z]" ignoreCase="false" />
</conditions>
<action type="Redirect" url="{ToLower:{R:1}}" RedirectType="Permanent"/>
</rule>

Resources