4532066
4532066

Reputation: 2110

IIS Rewrite Rule - How to include root documents

I have a simple IIS rule to redirect HTTPS to HTTP:

<rule name="HTTPS" enabled="true" stopProcessing="true">
    <match url=".*\.(asp)$" ignoreCase="false" />
    <conditions>
        <add input="{HTTPS}" pattern="on" />
        <add input="{REQUEST_URI}" negate="true" pattern="^/ecards/user*" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="http://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

The matching url will only work on .asp files, but how can I also get it to work for root directories?

E.g.

example.com
example.com/
example.com/test
example.com/test/

I don't want to just have the match URL as:

<match url="(.*)" />

Because then other non .asp files get rewritten.

Upvotes: 3

Views: 735

Answers (1)

Tasos K.
Tasos K.

Reputation: 8087

One way to include directories in your rule is to exclude everything else.

Assuming that everything else has a file extension (e.g. .php, .css. .js, etc.) you can negate all input that has the . in the path.

I changed your code a bit to make a working demo locally (I don't have HTTPS locally to test so instead of redirecting to HTTP I set it to redirect to About.aspx) and the two rules are:

<rule name="HTTPS" enabled="true" stopProcessing="true">
    <match url=".*\.(asp)$" ignoreCase="false" />
    <action type="Redirect" url="/About.aspx" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="NEWRULE" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{REQUEST_URI}" negate="true" pattern=".*\.(.)$" ignoreCase="true" />
        <add input="{REQUEST_URI}" negate="true" pattern="^/About*" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="/About.aspx" appendQueryString="true" redirectType="Permanent" />
</rule> 

So, based on your original code sample, a new rule that will work for you would be similar to this:

<rule name="IncludeDirectories" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{REQUEST_URI}" negate="true" pattern=".*\.(.)$" ignoreCase="true" />
        <add input="{HTTPS}" pattern="on" />
        <add input="{REQUEST_URI}" negate="true" pattern="^/ecards/user*" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="http://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>  

Note: The above approach is rather aggressive. You could replace this condition:

<add input="{REQUEST_URI}" negate="true" pattern=".*\.(.)$" ignoreCase="true" />

with the following:

<add input="{REQUEST_URI}" negate="true" pattern=".*\.(php|css|js|jpg|gif|png)$" ignoreCase="true" />

Where you exclude specific extensions. You add as many as you want.

Edit: If you want to have specific pages still with HTTPS maybe the following rule will be helpful (haven't tested it though). The previous rule sends to HTTP all URLs except those that have /ecards/user where this one sends to HTTPS those that have /ecards/user. I believe there will be no conflict.

<rule name="HTTPS2Admins" enabled="true" stopProcessing="true">
    <match url="/ecards/user(.*)" />
    <conditions>
        <add input="{HTTP}" pattern="on" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/ecards/user{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

Upvotes: 1

Related Questions