Umbraco https rewrite rule causes an infinite loop
Asked Answered
D

4

8

I have the following rewrite rule that works perfectly fine on a regular asp.net project, running on IIS7.

<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>

So one of our pages when visited at http://{domain}/aboutus will redirect to https://{domain}/aboutus. Now putting the same rewrite rule in an Umbraco site causes an infinite loop. We don't have any other rewrite rule for our Umbraco site. That leads me to think that Umbraco is somewhat hijacking the routing from http to https and causes the infinite loop. What are we missing?

Denbrook answered 23/12, 2014 at 0:46 Comment(2)
Which version of Umbraco are you using?Triviality
Hi PTuckey, it's 7.1.4Denbrook
P
2

I recommend using the following rule instead:

<rule name="Redirect to https" 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="false" />
</rule>
Padauk answered 29/12, 2014 at 10:11 Comment(4)
I have seen you answered this in another thread and I have actually tried this and this does not work. Again, the rewrite rule works already in a regular asp.net website but not with an Umbraco site.Denbrook
I awarded the bounty to this answer as this came closest to the solution I need. I tested this, along with the rewrite in my question, and both worked on a fresh install of Umbraco. However, my problem still remains.Denbrook
If you need more help you should let me know what "doesn't work" :) Umbraco does no other redirects by itself and I'm using this rewrite rule sucessfully. I think it requires you NOT to have any rewrites in urlRewriting.config and this should be the first rewrite rule in your web.config file (as it stops processing after that). our.umbraco.org is a better place to discuss.Padauk
There are no rewrites in both config files except for the one you suggested. Now I'm suspecting one of the plugins we installed causes the infinite loop. I am slowly rebuilding the site. I'll head over there. Thanks again.Denbrook
O
2

As your regex for url isn't filters the input (<match url="(.*)" />), you should use redirectType="Permanent" parameter in your code:

More information can be found here:
Add an Url Rewrite rule

One thing worth noting is that by default the re-directs are 302 re-directs, if you want to do 301 re-directs you need to add the following:
redirectMode="Permanent"
You can find the full instructions for the URL re-writing component on their website: https://github.com/aspnetde/UrlRewritingNet

Ocana answered 29/12, 2014 at 10:17 Comment(1)
I tried different redirectType before I posted my question and it does not help. Again, the rewrite rule works already in a regular asp.net website but not with an Umbraco site. So the rewrite rule must be correct right? So must be with a configuration in Umbraco?Denbrook
T
1

One potential solution is to use the Umbraco rewrite module rather than an IIS rewrite.

In the URL rewriting config file (Config/UrlRewriting.config), the following rule is a simple example of how to redirect from HTTP to HTTPS:

<add name="https Rewrite"
    redirect="Domain"
    redirectMode="Permanent"
    virtualUrl="http://(.*)"
    destinationUrl="https://$1"
    ignoreCase="true" />

This rule should be placed within the <rewrites> section.

Edit: As per sebastiaan's comment, the urlRewriting.net module is outdated and an IIS solution should be used where possible.

Triviality answered 30/12, 2014 at 9:9 Comment(1)
Unfortunately the urlRewriting.net module is very old and pretty crappy. We only keep it around because some people can't install the IIS rewrite plugin. I would always advise using the IIS one if at all possible.Padauk
N
0

I also have had issues with infinite redirect loops with Umbraco 6.2.4. It was occurring randomly every week or so. My site is all SSL, with UmbracoUseSSL = false.

My HTTP to HTTPS rules was as follows:

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

I updated to the following per @sebastiaan

<rule name="Redirect to https" 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="false" />

Sounds like since R:1 can contains an empty string, it has the potential to cause loops. {REQUEST_URI} will always contain at least a slash. Not sure if that is why @sebastiaan recommends the latter?

If the problem persists, I'll report back. Hopefully the updated rule will resolve the issue.

Nils answered 8/1, 2015 at 22:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.