How to dynamically rewrite/redirect url - url

I got a little bit of a complicated problem here. I searched other topics but did not come across a solution.
I have an website-url, say foo.com, and an url called bar.com. They share the same code. Now everything works great, except for the redirecting the URL part. I also do want to let you guys know I've had no experience with URL Rewriting, so keep it simple please.
Foo.com existed first, and got redirected from foo.com to www.foo.com via a CanonicalHostNameRule, redirecting the pattern (.*)to http://www.foo.com/{R1}, which works great for that domain, but not for the bar.com domain.
Below are my full rewrite rules for the website:
And these are my web.config rewrite rules:
<rules>
<clear />
<rule name="LetsEncrypt Rule" stopProcessing="true">
<match url="^\.well-known.*$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
<rule name="CanonicalHostNameRule1" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www.\foo.be$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.{R:2}/{R:1}" />
</rule>
<rule name="Rewrite legacy url's" patternSyntax="ECMAScript">
<match url="^.((?!nl-BE).)+$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" redirectType="Permanent" />
</rule>
<rule name="One homepage - redirect /home/">
<match url="^([a-z][a-z]-[A-Z][A-Z])/home/?" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}/" redirectType="Permanent" />
</rule>
<rule name="One homepage - redirect root to language">
<match url="^\d*$" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="/nl-BE/" redirectType="Permanent" />
</rule>
<rule name="Add trailing slash">
<match url="^([^.]*[^/])$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}/" redirectType="Permanent" />
</rule>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
Can you guys please get me on the right track on how to fix this? This is all to complicated for my small brain. Thanks in advance guys!

you could try below url rewrite rule:
<rule name="Canonical3" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^([a-z0-9]+[.]com)$" />
</conditions>
<action type="Redirect" url="http://www.{C:1}/{R:0}" redirectType="Permanent" />
</rule>
this will work with both the domain name.

Related

IIS with several bindings i need redirect one to HTTPS

We have a website with multiples bindings/domains. I need to redirect one of them to HTTPS. I try some rules with URL Rewrite but it doesnt work:
rule name="HTTPS Redirect" stopProcessing="true">
<match url="^www.domain.com$" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" />
</rule>
OR
rule name="HTTPS Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.domain.com$" />
</conditions>
<action type="Redirect" url="https:www.domain.com" />
</rule>
And doesnt work.
Any advise?
Thanks,
Try to combine these two rules.
rule name="HTTPS Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.domain.com$"/>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>

URL rewrite rule (iis): redirect ONLY www.example.com and www.example.com/default.aspx

I am trying to do a rewrite to redirect the following pages ONLY:
www.example.com/
www.example.com/default.aspx
I want to let all other requests pass through. I know I'm pretty close but I just can't seem to get it perfect. Thanks in advance for any & all help!
Here's what I have so far:
<rewrite>
<rules>
<rule name="Base Redirect" stopProcessing="true">
<match url="" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" ignoreCase="true" negate="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="shop.example.com" appendQueryString="true" />
</rule>
<rule name="Default.aspx Redirect" stopProcessing="true">
<match url="default.aspx" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" ignoreCase="true" negate="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="shop.example.com" appendQueryString="true" />
</rule>
</rules>
</rewrite>
Problem is it's sending anything containing default.aspx (/pages/default.aspx, etc) and we don't want to redirect anything but the root level default.aspx.
Felix
Try this (basically adding the "^" character to tell regex to only match when it is at the beginning:
<rule name="Base Redirect" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" />
</conditions>
<action type="Redirect" url="http://shop.example.com/" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Default.aspx Redirect" stopProcessing="true">
<match url="^default.aspx" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" />
</conditions>
<action type="Redirect" url="http://shop.example.com" appendQueryString="true" redirectType="Permanent" />
</rule>

Too Many Redirects IIS URL Rewrite with multiple rules

I'm getting too many redirects with my two rules in IIS. One rule is to redirect all traffic to HTTPS, the other is to redirect to a 'not supported' page for browsers that don't fit my mapped criteria - this page is at the top level of the app. I am trying to exclude this 'NotSupported' page from the HTTPS rule as I believe this is what is causing all the redirects - but, it's not working... Anyone see what I'm doing wrong?
Thanks
<rewrite>
<rules>
<rule name="UserAgent MSIE Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="Chrome|MSIE 8.0|MSIE 7.0b|MSIE 7.0|MSIE 6.0b|MSIE 6.0|MSIE 5.5b1|MSIE 5.5|MSIE 5.0|MSIE 5.01|MSIE 4.0" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/NotSupported.html" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" negate="true" pattern="^/NotSupported\.html$" ignoreCase="true" />
<add input="{HTTPS}" pattern="^OFF$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
EDIT:
This works (redirecting to Google)...
<rule name="UserAgent MSIE Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{REQUEST_URI}" negate="true" pattern="^/notsupported$" ignoreCase="true" />
<add input="{HTTP_USER_AGENT}" pattern="Chrome|MSIE 8.0|MSIE 7.0b|MSIE 7.0|MSIE 6.0b|MSIE 6.0|MSIE 5.5b1|MSIE 5.5|MSIE 5.0|MSIE 5.01|MSIE 4.0" />
</conditions>
<action type="Redirect" url="http://www.google.com/" appendQueryString="false" redirectType="Found" />
</rule>
But this doesn't - trying to use an internal address. How can I exclude this rule on that page so it stops the loop?
<rule name="UserAgent MSIE Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{REQUEST_URI}" negate="true" pattern="^/notsupported$" ignoreCase="true" />
<add input="{HTTP_USER_AGENT}" pattern="Chrome|MSIE 8.0|MSIE 7.0b|MSIE 7.0|MSIE 6.0b|MSIE 6.0|MSIE 5.5b1|MSIE 5.5|MSIE 5.0|MSIE 5.01|MSIE 4.0" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/notsupported.html" appendQueryString="false" redirectType="Found" />
</rule>
Any ideas?

URL Rewrite update protocol and domain if URL is http://

I would like to redirect all calls to http://www.domain.com/csp/ to https://secure.doman.com/csp/ using URL rewrite running on IIS 8.5. I would like the URL on the browser to update accordingly. I cannot get it to work.
Below is the section of the web.config file with the rule
<rules>
<rule name="CSP to SSL" enabled="true" stopProcessing="true">
<match url="^http://www.domain.com/csp/(.*)$" />
<conditions>
<add input="{CACHE_URL}" pattern="^http://" />
</conditions>
<action type="Redirect" url="https://secure.domain.com/csp/{R:1}" />
</rule>
</rules>
Thank you.
Thanks to peterviola's reply at http://forums.iis.net/t/1214515.aspx?Change+to+https+and+adjust+domain+if+URL+uses+http+and+matches+a+particular+path I managed to get to the solution I was seeking.
<rule name="csp2HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)csp/(.*$)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="csp2subdomain" enabled="true" stopProcessing="true">
<match url="(.*)csp/(.*$)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www.|)domain.com$" />
</conditions>
<action type="Redirect" url="https://secure.domain.com{REQUEST_URI}" appendQueryString="true" redirectType="Permanent" />
</rule>
The above solution works. I can't understand why this could not be done using one rule.
Hope this helps.

Easy IIS 7.5 url rewrite rule (from non-www to www) gives redirect loop error in browser

This seems like it should be such a 101-level question but I cannot get it to work.
I very simply need to set up a url rewrite rule that redirects mydomain.com to www.mydomain.com. That's it. But no matter how I go at it, I get a URL redirect error in my browser.
Below are a few different rule definitions I have tried, one-by-one. As far as I can tell, any should work. But none have. I would be incredibly grateful for any insight.
<rule name="non-www to www" enabled="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
<add input="{HTTP_HOST}" negate="true" pattern="^www.mydomain.com$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="non-www to www" enabled="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>
<rule name="non-www to www" enabled="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>
In IIS, it's built in as a canonical host redirect. See below to 301 redirect any request from http://example.com to http://www.example.com
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>

Resources