HTTP to HTTPS Redirect Exception - http

I'm using a HTTP to HTTPS redirect in my MVC application.
This is the code that I'm using in Web.config:
<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>
There is a page in my application that doesn't work on HTTPS, so I need to add an exception to this rule.
I was wondering if anyone can help me on that or show me some tutorials/examples.

You just need to add another condition that negates the rule for your url. Your new rule would be something like this:
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{R:1}" pattern="myfolder\/myfile\.aspx" negate="true"/>
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}"/>
</rule>

Related

How to redirect url to ssl url on localhost in web.confg

localhost:5011 is url and localhost:44330 is SSL url. I want to redirect http://localhost:5011 to https://localhost:44330 in my web.config. I found rule for this but it is not worked as I wanted:
<rule name="Redirect local requests to https" stopProcessing="true">
<match url="(localhost:5011*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://localhost:44330/" redirectType="Permanent" appendQueryString="true" />
</rule>
This redirects to https://localhost:5011. How can I fix this?
I think your rul won't redirect Because<match url= ? doesn't contain hostname and port number.
If you need to redirect http://localhost:5011 to https://localhost:44330, please try this rule
<rule name="Redirect local requests to https" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="localhost" />
<add input="{SERVER_PORT}" pattern="^5011$" />
</conditions>
<action type="Redirect" url="https://localhost:44330/" redirectType="Permanent" appendQueryString="true" />
</rule>
IIs shouldn't redirect http://localhost:5011 to https://localhost:5011, so please check whether you have other rewrite rule or your application is doing the same work.

Possible to exclude subfolder from https rewrite?

I have a webiste that runs https. Now some programmers need a subfoder that has no https.
This is what i have in my web.config right now:
<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
We need the folder "wp-content\rest" to be excluded from this https redirect.
In hope someone can help me with this one.
You need to add an additional condition into your rule:
<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{REQUEST_URI}" pattern="^/wp-content/rest" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
Then this rule will disable redirects to all urls like that http://www.example.com/wp-content/rest*

http to https in IIS

I realize this is a simple question, but I'm just not finding the answer. I've applied the below rule...
<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>
The url I'm interested in has the pattern
http://[domain]/[directory]/[aspx page]
So http://example.com/funstuff/thefair.aspx
The result of the rewrite is http://[domain]/[an_aspx_page]
So the rewrite is removing the directory.
I assumed the {R:1} specifies a parameter that will be rewritten, and I tried https://{HTTP_HOST}/{R:1}/{R:2}, but this result in a 500 error.
I want to direct all traffic on this domain to https without changing the rest of the url entered by the user.
Here's what we use on one of our sites to auto-redirect all traffic to the https version. It also includes everything else in the url.
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>

IIS Re-write http to https with subfolders

As simple as it sounds and as much as I've searched, I'm not able to get
http://example.com/subfolder to redirect to https://example.com/subfolder using IIS rewrite.
Note: This is only when "http" is explicitly stated in the browser.
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />enter code here
Thanks!
The problem with your rule is that REQUEST_URI in your redirect action is the whole URI as requested. What you want is the result of the rule match. R1 will give you this. This answer gives a good explanation of the rule back-references. A working rule can be constructed like this:
<rewrite>
<rules>
<rule name="HTTP to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>

How do I redirect all but a single url to https in ASP.Net?

I have the following code in my web.config:
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
I would like to redirect everything EXCEPT for http://www.mysite.com/manual to https.
How would I modify the above to allow this?
It should be ok with adding the following code in your conditions tag
<add input="{REQUEST_URI}" negate="true" pattern="^/manual/*" ignoreCase="true" />

Resources