URL Rewrite and Web.config - url

i need to redirect from
www.domain.com to https://domain.com
http://www.domain.com to https://domain.com
http://domain.com to https://domain.com
I have the following lines of code and it works fine
<system.webServer>
<rewrite>
<rules>
<rule name="SecureRedirect" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
i take it from here
I want also rediredt from
https://www.domain.com to https://domain.com
i add the following lines
<rule name="Remove WWW" stopProcessing="true">
<match url="^https://www.(.*)" />
<action type="Redirect" url="http://{R:1}" />
</rule>
but it no redirect from https://www.domain.com to `https://domain.com
why?

You are the conditions snippet in between your <match> and <action> tags for https redirection.
Try this:
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^https://www.domain\.com$" />
</conditions>
<action type="Redirect" url="http://{R:1}" />
</rule>

Related

IIS using (URL Rewrite) to Redirect to another domain

trying to redirect from https://www.domain.info/cms-cp/login.html to https://www-server2.domain.info/cms-cp/login.html
by using the following code for (URL Rewrite) plugin, but unfortunately not working.
<rule name="Redirect To Web2 Server CMS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^(www.domain.info/cms-cp/login.html)$" />
</conditions>
<action type="Redirect" url="https://www-server2.domain.info/cms-cp/login.html" redirectType="Permanent" />
</rule>
<rule name="Redirect To Server2" patternSyntax="ECMAScript" enabled="true" stopProcessing="true">
<match url="login.html" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.domain\.info$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www-server2.domain.info/cms-cp/login.html" redirectType="Permanent" />
</rule>
{HTTPS} is just a bool value to check whether https has been enabled or not.
So you can apply the rule just like this
<rule name="Redirect To Web2 Server CMS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^on$" />
<add input="{HTTP_HOST}" pattern="www\.domain\.info" />
<add input="{URL}" pattern="^/cms-cp/login.html$" />
</conditions>
<action type="Redirect" url="https://www-server2.domain.info/cms-cp/login.html" />
</rule>

How to dynamically rewrite/redirect 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.

Redirect all URLs to contain 'www' prefix

I want to redirect all traffic from
https://url
to
https://www.url
How can I write the redirect url?:
<rewrite>
<rules>
<rule name="Add www prefix">
<match url="(.*)" ignoreCase="true" />
<action type="Redirect" url="what to put here?" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
have you tried doing like that
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.YOURWEBSITE.com" negate="true" />
</conditions>
<action type="Redirect" url="http://www.YOURWEBSITE.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
You can find more here
Try something like this :
<rules>
<rule name="www redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://www.{HTTP_HOST}/{R:1}" />
</rule>
</rules>
Adjust that to your needs but i think it works.

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>

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.

Resources