Web.config Redirect all to https and non-www - web-config

What I need to do is redirect all request to https and non-www url. For example:
-http:// example .com/something#hash --> https:// example .com/something#hash
-https://www. example .com/something#hash --> https:// example .com/something#hash
-http://www. example .com/something#hash --> https:// example .com/something#hash
What I did and doesn't work...:
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<!--<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />-->
<action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect to non-www" stopProcessing="true">
<match url="(.*)"></match>
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" negate="true"></add>
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
<outboundRules>
<rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
<match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
<conditions>
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
</conditions>
<action type="Rewrite" value="max-age=31536000" />
</rule>
</outboundRules>
</rewrite>
Thanks!!!

Try this:
<rule name="HTTP to HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>

Related

Two redirections instead of one for Lower Case

When I add the following rule to the redirection file:
<rule name="To Lowercase" enabled="true" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/Images/" negate="true" />
<add input="{REQUEST_METHOD}" pattern="GET" ignoreCase="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
<rule name="Redirect to WWW">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^foo.com$" />
</conditions>
<action type="Redirect" url="http://www.foo.com/{R:0}" redirectType="Permanent" />
</rule>
And attempt to open webiste "Foo.com" watching network behaviour I can see that it's first redirected (301) from "https://Foo.com" to "http://foo.com" and then from "http://foo.com" back to "https://foo.com" with status 302.
Is there a way to modify this rule to make just one redirection?
Based on your answers in comments, you can just simply specify the domain name in your rules to avoid multiple redirects. For example:
<rule name="To Lowercase" enabled="true" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/Images/" negate="true" />
<add input="{REQUEST_METHOD}" pattern="GET" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.foo.com/{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
<rule name="Redirect to WWW">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^foo.com$" />
</conditions>
<action type="Redirect" url="https://www.foo.com/{R:0}" redirectType="Permanent" />
</rule>

Redirect to https://www in web.config

I'd like both example.com and www.example.com to be redirected to https://www.example.com
At the moment, I have these two rules in my web.config:
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Root Redirect to www" stopProcessing="true">
<match url=".*" negate="false" />
<action type="Redirect" url="{C:1}://www.{HTTP_HOST}/{R:0}" />
<conditions trackAllCaptures="true">
<add input="{CACHE_URL}" pattern="^(.*)://" />
<add input="{HTTP_HOST}" pattern="^(?!www\.).*" />
</conditions>
</rule>
Is it possible to combine them, so the user only experiences one redirect?
Update:
This seems to do the trick:
<rules>
<clear />
<rule name="Redirect non-www OR non-https to https://www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^example.com$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent"/>
</rule>
</rules>
Any downsides?

IIS url redirect from non-www to www not working

I have this rewriting rule:
<rewrite>
<rules>
<rule name="Force non-WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.yourdomainname.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
It successfully redirects example.com to https://example.com, however, I need redirection to https://www.example.com, how can I do this?
try this ,just limit the domain or you want dynamic domain?
<rewrite>
<rules>
<rule name="Force non-WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" negate="true" pattern="^www.(.*)$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
use the match all and the negate to redirect

Rewrite Rules for HTTP to HTTPS Redirect

I am trying to redirect all http://thesite.com, http://www.thesite.com, https://thesite.com to https://www.thesite.com. But I don't have the pattern correct. What am I doing wrong?
<rewrite>
<rules>
<rule name="Redirect to HTTPs" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
I found the solution:
<rule name="Redirect non-www OR non-https to https://www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^thesite.com$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.thesite.com/{R:0}" redirectType="Permanent"/>
</rule>

Re-write old url to new url Asp.Net

I have 2 domains pointing to the same Umbraco application:
oldexample.com
newexample.com
I want my application to change the URL from:
oldexample.com/...
TO
newexample.com/...
when people visit the oldexample.com.
In my web.config, I've placed this, but to no effect:
<rewrite>
<rules>
<rule name="Redirect old-domain to new-domain" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^oldexample.com$" />
</conditions>
<action type="Redirect" url="http://www.newexample.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\." />
<add input="{HTTP_HOST}" negate="true" pattern="localhost" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
What am I doing wrong?
// The application is hosted in Azure web sites. And both URLs are hostnames assigned to the site.
<rewrite>
<rules>
<rule name="Redirect oldexample.com to newexample.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.oldexample\.com$" />
<add input="{HTTP_HOST}" pattern="^oldexample\.com$" />
</conditions>
<action type="Redirect" url="http://www.newexample.com/{R:1}" />
</rule>
</rules>
</rewrite>