8

I wanted to force all inbound connections to revert to HTTPS for my site. Searching StackOverflow.com for an answer yielded the following code:

<rewrite>
    <rules>
        <rule name="Redirect to HTTPS" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
        </rule>
     </rules>
</rewrite>

This works great. Now everyone coming in to http://mySite.com is redirected to https://mySite.com.

mySite.com has a sub-application which is accessed at mySite.com/ajax/ . I don't want inbound connections to this sub-application to be subject to the rewrite; The original protocol (http or https) that the user specifies should be maintained when connecting to the /ajax sub-application. What shall I place in this child application's web.config file to negate the rewrite that takes place at the parent level?

(The reason I want the /ajax child site to be exempt from the https rewrite is because it may be accessed from third-party HTTP-based web pages. If the third-party page that's making the call to /ajax is HTTP (not HTTPS), then I want to retain that HTTP'ness when the call is made.)

Chad Decker
  • 5,997
  • 8
  • 27
  • 31

1 Answers1

19

The following web.config snippet should do what you want. It removes all rewrite rules for this application.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <!-- Insert rules for this application (if any) **below** the "clear" -->
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
  • How can you do something similar if the two applications share the same webroot and config? – rickard Aug 23 '13 at 08:42
  • @rickard a comment isn't the right place to be asking that, because that's a different question. Try searching, or asking it as a new question. I'll give you one possibility here, though I don't know if it will work in your application. Can you convert the subfolder to its own application (right-click on the subfolder in the IIS manager and choose "Convert...". – Moshe Katz Aug 23 '13 at 13:54
  • 2
    @DenisHowe I don't know where it is documented for rewrite rules specifically, but it appears all over the place in IIS config. See https://msdn.microsoft.com/en-us/library/ms691347%28v=vs.90%29.aspx for one example. (I think that I originally discovered it by looking at a config file that was generated by the GUI.) – Moshe Katz Mar 19 '15 at 11:28
  • This helped me in a scenario where I am hosting OpPlan Api and React on the same server with IIS and React sets up custom rewrite rules and I needed to host that app on the root of IIS of Default Web Site, while clearing the rewrite rules for the .net core rest api application. Handy! – Tore Aurstad Sep 10 '18 at 17:10