Two redirections instead of one for Lower Case - redirect

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>

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>

Rewrite URL redirect

I am trying to re-write a URL from this address:
https://merchant.test-01.mysite.co/payment-gateway/vend?amount=69.90&register_id=12&currency=NZD
To this address:
https://mysite.co/nz/test-01/merchant/payment-gateway/vend?amount=69.90&register_id=12&currency=NZD
I try this:
<rule name="test01Generic" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(\S+?).test-01.mysite.co$" />
</conditions>
<action type="Redirect" url="https://mysite.co/nz/test-01/{C:1}" redirectType="Found" />
</rule>
With this result:
https://mysite.co/nz/test-01/merchant?amount=69.90&register_id=12&currency=NZD
Where payment-gateway/vend is missing
And this
<rule name="test01Generic" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(\S+?).test-01.mysite.co$" />
</conditions>
<action type="Redirect" url="https://mysite.co/nz/test-01/{C:1}{REQUEST_URI}" redirectType="Found" />
</rule>
With this result:
https://mysite.co/nz/test-01/merchant/payment-gateway/vend?amount=69.90&register_id=12&currency=NZD&amount=69.90&register_id=12&currency=NZD
The query parameters double up.
Any ideas on what am I missing?
Please modify {REQUEST_URI} to {URL} and leave appendQueryStrin to true, then it would work.
<rule name="test0Generic" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(\S+?).test-01.mysite.co$" />
</conditions>
<action type="Redirect" url="https://mysite.co/nz/test-01/{C:1}{URL}" appendQueryString="true" redirectType="Found" />
</rule>
I was able to work it out, I had to change {REQUEST_URI} to {PATH_INFO}
<rule name="test01Generic" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(\S+?).test-01.mysite.co$" />
</conditions>
<action type="Redirect" url="https://mysite.co/nz/test-01/{C:1}{PATH_INFO}" redirectType="Found" />
</rule>
Now when the address bellow is called:
https://merchant.test-01.mysite.co/payment-gateway/vend?amount=69.90&register_id=12&currency=NZD
I'm redirecting to this address:
https://mysite.co/nz/test-01/merchant/payment-gateway/vend?amount=69.90&register_id=12&currency=NZD

IIS Rewrite / Redirect Rules Query

The rule below does the following:
- http://www.example.com -> https://example.com
etc
- https://example.com/contacts.html -> . https://example.com/contacts
So (all requests to HTTPS, and all www -> (non)www) + hides .HTML extensions.
I would like to add the ability to remove the following issue:
https://example.com/Home.html -> https://example.com/Home
But for the specific case of /Home I would prefer it displays /
Any help would be greatly appreciated..
Rule below:
<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://example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Redirect .html extension" stopProcessing="false">
<match url="^(.*)\.html$" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*)\.html$" ignoreCase="false" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="hide .html extension" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<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>
</rules>
</rewrite>
I made some change in your rule to prevent rewrite for /Home and /. Then created two rules to handle request to /Home.html.
It works fine on my side and should achieve your requirement.
<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://example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Redirect .html extension" enabled="true" stopProcessing="true">
<match url="^(.*)\.html$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{URL}" pattern="(.*)\.html$" ignoreCase="false" />
<add input="{URL}" pattern="^/Home.html$" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="hide .html extension" enabled="true" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.html" matchType="IsFile" />
<add input="{URL}" pattern="^/$" negate="true" />
<add input="{URL}" pattern="^/Home$" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.html" />
</rule>
<rule name="redirect /Home" enabled="true" stopProcessing="true">
<match url="^Home.html$" />
<action type="Redirect" url="/" />
</rule>
<rule name="rewrite Home" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="/Home.html" />
</rule>
</rules>
</rewrite>

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?

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>