IIS7 URL rewrite rules makes friendly url recursive - friendly-url

I have a site done in asp and url rewritten rules from a db provider. I works fine but I have an annoying issue I've not been able to solve correctly.
The rewritten rules work as follows:
www.mydomain.com/default.asp --> www.mydomain.com/lang
www.mydomain.com/content.asp?id=1 --> www.mydomain.com/lang/category-title
www.mydomain.com/content.asp?id=1&ref=2 --> www.mydomain.com/lang/category-title/content-title
The rules are:
<rules>
<clear />
<rule name="dbp_Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{DB:{R:1}?{QUERY_STRING}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" logRewrittenUrl="false" />
</rule>
<rule name="dbp_Reverse" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{DB_rewrite:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" logRewrittenUrl="false" />
</rule>
</rules>
<outboundRules>
<clear />
<rule name="Outbounddbp_Reverse" preCondition="ResponseIsHtml1" enabled="true">
<match filterByTags="A" pattern="^(.*/)(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{DB_outbound:{R:2}}" matchType="Pattern" pattern="(.+)" ignoreCase="true" negate="false" />
</conditions>
<action type="Rewrite" value="{C:0}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
The problem is that when the user gets to
www.mydomain.com/lang/category-title/content-title
page all the links rewritten in this page goes like this:
www.mydomain.com/lang/category-title/content-title/*lang/category-title/content-title*
(as you can see the "lang/category-title/content-title" is repeated twice)
Sure I'm missing something in the rules but I cannot see what it is.
Any help would be appreciated.

Related

How to allow utm_source & utm_medium in IIS Rewrite / Reverse Proxy

I have created reverse proxy Windows server 2016 and IIS.
Everything is working fine, but the problem is following it doesnt allow the url like this,
https://mywebsite/shop/product/3231?utm_source=IGShopping&utm_medium=Social
but when I replace utm_source as utm_sour or something then it works fine.
In simple words, with utm_source and utm_medium variables it gives this error.
Screenshot of the error
Directly with the localhost it works fine.
https://mywebsite/shop/product/3231?utm_source=IGShopping&utm_medium=Social
For the reference, my web.config file is as follows
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<outboundRules>
<clear />
<rule name="Add SameSite" preCondition="No SameSite">
<match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
<action type="Rewrite" value="{R:0}; SameSite=None; Secure" />
</rule>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" enabled="true">
<match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:8068/(.*)" />
<action type="Rewrite" value="http{R:1}://mocauae.ae/{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
<preCondition name="No SameSite">
<add input="{RESPONSE_Set_Cookie}" pattern="." />
<add input="{RESPONSE_Set_Cookie}" pattern="; SameSite=None; Secure" negate="true" />
</preCondition>
</preConditions>
</outboundRules>
<rules>
<rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:8068/{R:1}" />
</rule>
<rule name="HTTPS" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="http://localhost:8068" redirectType="Found" />
</rule>
<rule name="HTTPS force" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://localhost:8068" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="https pure" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<action type="Redirect" url="https://localhost:8068" redirectType="Found" />
<conditions>
<add input="{HTTP}" pattern="off" />
</conditions>
</rule>
</rules>
</rewrite>
<httpRedirect enabled="false" destination="http://localhost:8068" exactDestination="true" />
</system.webServer>
</configuration>
I the solution by just simply ignoring the url parameter and it worked for me
<rule name="IngnoreUTMSource" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="utm_source=.*" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}" appendQueryString="false" redirectType="Permanent" />
</rule>

Site not redirecting from www to https

On IIS7, my website is not redirecting from www.example.com to https://example.com
Instead it tries to open https://www.example.com
Here is the web.config file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="WWW to HTTPS">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" />
</rule>
<rule name="HTTP TO HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
</rule>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="backend-api/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="^/(backend-api)/.*" />
</conditions>
<action type="Rewrite" url="http://localhost:3033/{R:1}" />
</rule>
<rule name="AngularPageRefresh" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
<outboundRules>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
What am I missing?
You can use the following rule:
<rule name="test1" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}" />
</rule>

IIS can't rewrite outbound rule having soket.io query

I am banging my head into the wall. Every single URL is re-written by IIS URL Rewrite module but the response having https://nginx-server/socket.io/?EIO=3&transport=polling&t=1486150196479-0 when I open my Network tab in chrome, I see:
https://nginx-server.com/socket.io/?EIO=3&transport=polling&t=1486150196479-0
https://nginx-server.com/socket.io/?EIO=3&transport=polling&t=1486150196479-0
https://iis-reverse-proxy-server.com/t/assets/images/chat-logo.png
https://iis-reverse-proxy-server.com/config.js
https://iis-reverse-proxy-server.com/t/assets/images/main_logo.png
I am trying to reverse proxy the https://nginx-server. IIS reverse proxy rewrite all the URL that are accessing nginx except those having socket.io URI in them . Same thing happens when some api is called and the IIS just stop rewriting outbound rules.
this is my web.config.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
<match url="^(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
<action type="Rewrite" url="{C:1}://nginx-server.com/{R:0}" />
</rule>
</rules>
<outboundRules>
<clear />
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" stopProcessing="true">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^(.*)?://nginx-server.com/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{R:1}://iis-reverse-proxy-server.com/{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>
</configuration>
==========Edit:===========
This is my updated web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
<match url="^(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<serverVariables>
<set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="HTTP_ACCEPT_ENCODING" />
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
<action type="Rewrite" url="{C:1}://nginx-server.com/{R:0}" />
</rule>
</rules>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" stopProcessing="true">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^(.*)?://nginx-server.com/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{R:1}://iis-reverse-proxy-server.com/{R:2}" />
</rule>
<rule name="RestoreAcceptEncoding" preCondition="NeedsRestoringAcceptEncoding">
<match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
<action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
</rule>
<rule name="Atag" preCondition="ResponseIsHtml1">
<match pattern="href=(.*?)https://nginx-server.com/(.*?)\s" />
<action type="Rewrite" value="href={R:1}https://iis-reverse-proxy-server.expertflow.com/{R:2}" />
</rule>
<rule name="elementencodedaction" preCondition="ResponseIsHtml1">
<match pattern="action=(.*?)https://nginx-server.com/(.*?)\\" />
<action type="Rewrite" value="‘action={R:1}https://iis-reverse-proxy-server.expertflow.com/{R:2}\" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)" />
</preCondition>
<preCondition name="NeedsRestoringAcceptEncoding">
<add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
Where am I making mistake?
Thanks everyone for the efforts for intended help. It turned out the config file in the application re writing the URL so that is why IIS could not be able to rewrite the URL. Updating URL in config file resolved my issue.

How do I get IIS UrlRewrite to handle CSS-delivered woff files appropriately?

Context: Azure; Windows Server 2012; IIS 8
First up, here's the (redacted) web.config for reference
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="false">
<match url="(.*)" />
<action type="Rewrite" url="https://www.khatam.com/{R:1}" logRewrittenUrl="true" />
<serverVariables>
<set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
<rule name="Capture Http Origin Header" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_ORIGIN}" pattern=".+" />
</conditions>
<serverVariables>
<set name="CAPTURED_ORIGIN" value="{C:0}" />
</serverVariables>
<action type="None" />
</rule>
</rules>
<outboundRules>
<clear />
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="None" pattern="^http(s)?://www.khatam.com/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="http{R:1}://jamuni.pemaish.com.au/{R:2}" />
</rule>
<rule name="Rewrite mundrjatzxera Assets" preCondition="ResponseIsHtml1" enabled="true">
<match filterByTags="None" pattern="^/(mundrjat/zxera/.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="https://www.khatam.com/{R:1}" />
</rule>
<rule name="Rewrite Source Srcset in Picture Assets" preCondition="ResponseIsHtml1" enabled="true">
<match filterByTags="CustomTags" customTags="Source Srcset in Picture" pattern=",?\/(mundrjat\/zxera\/\S+\s\d+w)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="https://www.khatam.com/{R:1}" />
</rule>
<rule name="Rewrite X-Frame-Options" enabled="true" patternSyntax="Wildcard">
<match serverVariable="RESPONSE_X-Frame-Options" pattern="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" />
</rule>
<rule name="Set-Access-Control-Allow-Origin for known origins" enabled="true">
<match serverVariable="RESPONSE_Access-Control-Allow-Origin" pattern=".+" negate="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{CAPTURED_ORIGIN}" />
</rule>
<rule name="Restore Accept Encoding" preCondition="Needs to Restore Original Accept Encoding" enabled="true">
<match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
</rule>
<preConditions>
<preCondition name="ResponseIsCss">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/css" />
</preCondition>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
<preCondition name="ResponseIsEverything">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)$" />
</preCondition>
<preCondition name="Needs to Restore Original Accept Encoding">
<add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".*" />
</preCondition>
</preConditions>
<customTags>
<tags name="Source Srcset in Picture">
<tag name="source" attribute="srcset" />
</tags>
</customTags>
</outboundRules>
</rewrite>
...
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
We're reverse proxying a site for a client. The proxied site is www.khatam.com. The server through which the proxying is effected is jamuni.pemaish.com.au (yes, I do speak Urdu, albeit not brilliantly.) The client will have an IFRAME in their site which will interact with khatam.com's site via our jamuni server.
So far so good: the client is able to run everything in khatam's site via their IFRAME. What's NOT working so well is that there are WOFF files referenced in the one of the CSS files and these are not loading.
When in devtools in a browser (which is rendering the IFRAME) all the woffs are in red, with the General headers being
Request URL: https://jamuni.pemaish.com.au/vgera.mukljuga/jugabisbis/mukljuga/khatam/vesael/icomoon.woff
Request Method: GET
Status Code: 500 URL Rewrite Module Error.
Remote Address: XXX.XXX.XXX.XXX:443
Referrer Policy: no-referrer-when-downgrade
I have tried to write a rule to change the CSS, viz
<rule name="Rewrite vgera.mukljuga Assets" preCondition="ResponseIsCss" enabled="true">
<match filterByTags="None" pattern="url\((khatam/vesael/.*?.woff)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="url(https://www.khatam.com/vgera.mukljuga/jugabisbis/mukljuga/{R:1}" />
</rule>
but whether the target or the intermediate is specified, I still get the 500 error. Now as I re-read this I'm wondering if I'm having the output of one rewrite being picked up by another leading to a loop or a race. If I take the link from the General above and put it into the address bar of the browser, I get a woff file suggesting that there's contention between two or more rules.
The CSS rules are relative-pathed. Here's one of them:
#font-face {
font-family: trade-gothic-condensed;
src: url(khatam/vesael/tradegothicltcom-bdcn20-webfont.woff) format("woff");
font-weight: 700;
font-style: normal;
-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}
Suggestions welcome.
In the process of getting the Tracing role installed, the 500 error above fixed itself suggesting that the issue had more to do with (likely user-introduced) IIS instability rather than anything else.

One redirect to rule them all

After trying to help a fellow developer on the iis.net forums, I began to search for a better way to do my redirects. And thus I ended up with this article.
I took a lot of these ideas and began to implement my own version of it. Everything seemed to work just okay, but I hit a bump in the road and I hope you can help.
Okay so, a quick explanation on my rules:
My idea is to test the url, and if I find something wrong with it, I rewrite the url and let it go on, rather than redirect it instantly. If at any point I rewrite the url, I set a custom server variable "Redirect" to true. Then in the end, I test if my custom server variable is true and redirect the user.
The point of it is to only have one 301 redirect, rather than a chain of redirects.
These are my rules (Sorry for the wall):
<rules>
<rule name="WhiteList - resources" stopProcessing="true">
<match url="^resources/" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
<rule name="Redirect subdomains with www to non-www" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
<add input="{HTTP_HOST}" pattern="^www\.(.*)\.([^\.]+)\.([^\.]+)$" />
</conditions>
<action type="Rewrite" url="http://{C:1}.{C:2}.{C:3}{HTTP_URL}" />
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="Redirect top domains with non-www to www" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
<add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
</conditions>
<action type="Rewrite" url="http://www.{HTTP_HOST}{HTTP_URL}" />
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="SEO - Remove trailing slash" stopProcessing="false">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}" />
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="SEO - ToLower" stopProcessing="false">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="[A-Z]" ignoreCase="false" />
<add input="{URL}" pattern="^.*?\.(axd|css|js|jpg|jpeg|png|gif|ashx|asmx|svc).*?$" negate="true" />
<add input="{URL}" pattern="^.*/(webshop)/.*$" negate="true" />
</conditions>
<action type="Rewrite" url="{ToLower:{R:1}}" />
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="SEO - remove default.aspx" stopProcessing="false">
<match url="(.*?)/?default\.aspx$" />
<action type="Rewrite" url="{R:1}" />
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="SEO - Trim aspx" stopProcessing="false">
<match url="(.*)\.aspx$" />
<action type="Rewrite" url="{R:1}" />
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="SEO - non-canonical redirect" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{Redirect}" pattern="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
<serverVariables>
<set name="Redirect" value="false" />
</serverVariables>
</rule>
</rules>
Now most of it actually works pretty well, but I seam to have some issues with a naked domain.
If I take a subdomain with www (which should be redirected to non-www) then it appears to fail. Funny enough, if I go to a subpage, it works fine.
(I can't post more urls because I'm new here :( And I wanted to give credits to the others for starting me on this.)
I've traced it down to the server variable {HTTP_URL} because if I remove it, it doesn't fail. However it of course doesn't do what it is supposed to either (It always redirects to the naked domain). I've tried with various variables: {URL}, {REQUEST_URI} and they all seem to end up with the same error, which is getting a bit annoying.
Should anyone have some improvement for the rules, feel free to respond as well, I love working with them and I would like to make the near perfect redirects, so any suggestions are welcome.
This ended up being more of a version 1 and I have since improved on it to avoid the server variable.
Now I use a series of rewrite rules that transforms the url to something I like, appending underscore.
Then stripping the underscore and then redirecting.
It have worked very well for me for quite some time now and is being used by the company on almost all projects.
It have been made into a nuget package for easy setup.
http://www.nuget.org/packages/RedirectRules/
I believe I actually found the answer now. I worked around with it for some time and ended up with another issue, again with the www vs non-www. It simply didn't do anything.
So I ended up putting it at the end and making that redirects rather than rewrites as well. Apparently that works fine, why? No idea.
cheesemacfly suggested that I match on ^(.+)$ rather than (.*).
I thought it would do the very same thing, but realized that the match isn't on the hostname at all, it is everything after. Big surprise for me there, but it did open up a new possibility:
Instead of matching {URL}, {HTTP_URL} or some other variant of almost the same thing, I could simply refer to the match with {R:1}
I have put in two things since the first post. I put in conditions on some of the rules to exclude them if it contains "umbraco" as I work with that system and most of the rules I don't want to apply to the back-end. I also applied a rewrite map for SSL check, so it automatically puts you to https:// or http:// using the server variable {HTTPS}
So far I haven't had found any bugs in it, so I will mark this as solved and give you my last revision of the rules. Enjoy :)
<rewrite>
<rules>
<rule name="WhiteList - resources" stopProcessing="true">
<match url="^resources/" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
<rule name="SEO - Remove trailing slash" stopProcessing="false">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}" />
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="SEO - ToLower" stopProcessing="false">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="[A-Z]" ignoreCase="false" />
<add input="{URL}" pattern="^.*?\.(axd|css|js|jpg|jpeg|png|gif|ashx|asmx|svc).*?$" negate="true" />
<add input="{URL}" pattern="^.*/(webshop)/.*$" negate="true" />
<add input="{URL}" pattern="^/umbraco/" negate="true" />
</conditions>
<action type="Rewrite" url="{ToLower:{R:1}}" />
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="SEO - remove default.aspx" stopProcessing="false">
<match url="(.*?)/?default\.aspx$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="^/umbraco/" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}" />
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="SEO - Trim aspx" stopProcessing="false">
<match url="(.*)\.aspx$" />
<action type="Rewrite" url="{R:1}" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="^/umbraco/" negate="true" />
</conditions>
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="Redirect subdomains with www to non-www" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
<add input="{HTTP_HOST}" pattern="^www\.(.*)\.([^\.]+)\.([^\.]+)$" />
</conditions>
<action type="Redirect" url="{MapSSL:{HTTPS}}{C:1}.{C:2}.{C:3}/{R:1}" redirectType="Permanent" />
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="Redirect top domains with non-www to www" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
<add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
</conditions>
<action type="Rewrite" url="{MapSSL:{HTTPS}}www.{HTTP_HOST}/{R:1}" />
<serverVariables>
<set name="Redirect" value="true" />
</serverVariables>
</rule>
<rule name="SEO - non-canonical redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{Redirect}" pattern="true" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent"/>
<serverVariables>
<set name="Redirect" value="false" />
</serverVariables>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapSSL" defaultValue="OFF">
<add key="ON" value="https://" />
<add key="OFF" value="http://" />
</rewriteMap>
</rewriteMaps>
</rewrite>
There is a caveat though: Notice that a custom server variable is used. You cannot use a custom server variable out of the box. You need to add this variable to the <allowedServerVariables/> element in the applicationHost.config file, which is a global configuration file for IIS. (Source)
If you're using shared hosting (Including Azure Web Sites), chances are that you will not be able to change this setting.
If you're using an Azure Web Role, you'll need to add a startup task that modifies the aforementioned configuration file. If you don't do this, you'll get an IIS 500 error.
If someone have suggestions as to what could make the rules better, they are still welcome.

Resources