web.config merging 2 rules into 1 - web-config

I have 3 rules that acted for the basis of
1. Hiding HTML extension
2. Redirecting html
3. non-www to www.
However, this caused 2 301 redirections when user goes to
http://example.net/content -> http://www.example.net/content.html -> http://www.example.net/content
<rule name="Hide .html ext">
<match ignoreCase="true" url="^(.*)"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
<add input="{REQUEST_FILENAME}.html" matchType="IsFile"/>
</conditions>
<action type="Rewrite" url="{R:0}.html"/>
</rule>
<rule name="Redirecting .html ext" stopProcessing="false">
<match url="^(.*).html"/>
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*).html"/>
</conditions>
<action type="Redirect" url="{R:1}"/>
</rule>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.net$" />
</conditions>
<action type="Redirect"
url="{MapProtocol:{HTTPS}}://www.example.net/{R:1}" />
</rule>
I have a special requirement to only let it do 1 301 redirection for this page. I have tried multiple ways but no results. What is the best way i can try this?
Thank you!

Add as second rule :
<rule name="Redirect to www + .html ext" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.net$" />
<add input="{URL}" pattern="(.*).html"/>
</conditions>
<action type="Redirect"
url="{MapProtocol:{HTTPS}}://www.example.net/{R:1}" />
</rule>

Related

Syntax in IIS to use re-write subdomain as path of {HTTP_HOST}

Suppose I have a site: https://sub.domain.example, what is the proper syntax to get this to become https://domain.example/sub
The need is regardless of the sub and the Toplevel domain (e.g., .com,.net,.org) to have the redirect push the user to https://domain.example/sub
Here is what I have tried but no go--
<rules>
<rule name="SubDomain to Subdirectory">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(\w+)\.(.*)$" />
</conditions>
<action type="Rewrite" url="https://domain.example/{C:1}" />
</rule>
</rules>
You could try the below rule to achieve your requiremnet:
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="on" />
<add input="{HTTP_HOST}" pattern="([_0-9a-z-]+)\.(.*)" />
</conditions>
<action type="Redirect" url="https://domain.example/{C:1}" />
</rule>

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>

IIS7.5 redirecting non www to www also with https

I am trying to configure my IIS to redirect all none www calls to www calls including https
That first part is quite easy, but i have not found a solution that will also work when you have https enabled on your bindings.
So I would like both
http://domain.com/
and
https://domain.com/
and
http://www.domain.com/
to all be redirected to :
https://www.domain.com/
any suggestions?
#Josh: Your answer works for the first request.
It will end up with "https://www", perfect.
But if i then remove "www." from the address, it will fail like the image below.
<rewrite>
<rules>
<rule name="Non www HTTP to HTTPS" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^www" negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="Non www HTTPS to www HTTPS" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="On" />
<add input="{HTTP_HOST}" pattern="^www" negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="www*" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
</rules>
</rewrite>
This ended up working for me:
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>

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>

Web.config redirect one domain to non-https folder and another domain to https folder

I have a domain alias for my site. I would like to know how to redirect requests for domainA.ext to https://domainA.ext/folderA and requests for domainB.ext to http://domainB.ext/folderB
Presently I have the following rule to redirect all http requests to https but it redirects ALL requests to https:
<rule name="Redirect to https" stopProcessing="true">
<match url="(.mydomain.ext*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://mydomain.ext}" redirectType="Permanent" />*
</rule>
It is Windows server 2008, but my cms is in PHP.
I can't think of something more simple than 4 different rules.
The 2 first ones for domainA.ext:
<rule name="Check path folderA" stopProcessing="true">
<match url="^folderA" negate="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="domainA\.ext$" />
</conditions>
<action type="Redirect" url="https://domainA.ext/folderA/" />
</rule>
<rule name="Check SSL for domainA" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="domainA\.ext$" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://domainA.ext/folderA/" />
</rule>
1st rule: if the path doesn't start with folderA, then it redirects to https://domainA.ext/folderA/
2nd rule: if HTTPS is off, it redirects to https://domainA.ext/folderA/
And the 2 next ones for domainB.ext:
<rule name="Check path folderB" stopProcessing="true">
<match url="^folderB" negate="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="domainB\.ext$" />
</conditions>
<action type="Redirect" url="http://domainB.ext/folderB/" />
</rule>
<rule name="Check no SSL for domainB" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="domainB\.ext$" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://domainB.ext/folderB/" />
</rule>
1st rule: if the path doesn't start with folderB, then it redirects to http://domainB.ext/folderB/
2nd rule: if HTTPS is on, it redirects to http://domainB.ext/folderB/