Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bruno Olsen 75 posts 316 karma points
    Oct 19, 2017 @ 08:47
    Bruno Olsen
    0

    Rewrite rules: Redirect to https except one url

    Hi

    I'm trying to get some redirect to work :)

    I have a site, www.mysite.dk

    I have a rewrite rule in place in web.config to redirect http to https, and it looks like this:

    <rewrite>
        <rules>
            <rule name="Redirect to http" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*" negate="false" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTPS}" pattern="off" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
            </rule>
        </rules>
    </rewrite>
    

    Now, this works fine - however, I need to exclude the page www.mysite.dk/nothttps/ from the rule so it will remain on http.

    I've looked for at solution, of course, and I've found one that looked promissing but resulted in "too many redirects". That was to add this line after the first add-statement:

    <add input="{REQUEST_URI}" negate="true" pattern="^/nothttps/$" ignoreCase="true" />
    

    Obviously I'm doing it wrong, either in the input parameter or the pattern parameter - or both :D

    Is there anyone who can tell me how to do it right? :)

    Help would be much appreciated :)

    Kind regards, Bruno

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Oct 19, 2017 @ 08:59
    Nik
    0

    Hi Bruno,

    If you add that additional input to your https re-write rule.. you will need to change it from MatchAny to MatchAll else you create the conflict you are seeing.

    Nik

  • Bruno Olsen 75 posts 316 karma points
    Oct 20, 2017 @ 10:04
    Bruno Olsen
    0

    Hi Nik

    Thanks :)

    First hurdle overcome, it doesn't error with "too many redirects" :)

    So now it looks like this:

    <rewrite>
        <rules>
            <rule name="Redirect to http" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*" negate="false" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTPS}" pattern="off" />
                    <add input="{REQUEST_URI}" negate="true" pattern="^nothttps/" ignoreCase="true" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
            </rule>
        </rules>
    </rewrite>
    

    I've tried with the pattern being ^/nothttps/$ then ^/nothttps/ and now ^nothttps/ and it still redirects http://www.mysite.dk/nothttps/ to https://www.mysite.dk/nothttps/

    Anyone have an idea of what I'm doing wrong? :)

    Kind regards, Bruno

    EDIT: Also tried ^/nothttps$ still redirects

    EDIT2:

    Tried a different approach that still didn't work:

    <rewrite>
        <rules>
            <rule name="No redirect" stopProcessing="false">
                <match url="^~/infoskaerm$" ignoreCase="true" />
                <action type="None" />
                </rule>
            <rule name="Redirect to http" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*" negate="false" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTPS}" pattern="off" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
            </rule>
        </rules>
    </rewrite>
    

    (and it didn't help either to change false to true for the stopProcessing parameter)

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Oct 20, 2017 @ 16:01
    Nik
    0

    Hi Bruno,

    Try this:

        <rule name="Redirect to http" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="off" />
                <add input="{URL}" pattern="^/nothttps" negate="true" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
    

    I've tested this on a local site and it appears to work (although I did have the domain hard coded instead of using HTTP_HOST in the redirect action.

  • Bruno Olsen 75 posts 316 karma points
    Oct 24, 2017 @ 06:32
    Bruno Olsen
    100

    Hi Nik

    Sorry I didn't get back to you earlier. I actually found a solution just before you posted this. I needed to set ExactMatch:

    <rewrite>
        <rules>
            <rule name="No redirect" patternSyntax="ExactMatch" stopProcessing="true">
                <match url="/nothttps" ignoreCase="true" negate="true" />
                <action type="None" />
                </rule>
            <rule name="Redirect to http" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*" negate="false" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTPS}" pattern="off" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
            </rule>
        </rules>
    </rewrite>
    

    Thank you for your time and effort :)

    Kind regards, Bruno

  • Bruno Olsen 75 posts 316 karma points
    Nov 01, 2017 @ 10:39
    Bruno Olsen
    0

    Hi again

    I was a bit quick to say I found the solution. Actually now no http to https redirects happen.

Please Sign in or register to post replies

Write your reply to:

Draft