Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 232 posts 902 karma points c-trib
    Sep 22, 2022 @ 07:55
    Martin Rud
    0

    Umbraco 9 http to https redirect doesn't work

    Hi,

    In appsettings.json I have added:

    {
        "Umbraco": {
            "CMS": {
                "Global": {
                    "UseHttps": true
                }
            }
        }
    }
    

    but it doesn't work.

    How to fix? And can I have www to non-www redirecting also in appsettings.json?

  • Corné Hoskam 80 posts 587 karma points MVP 2x c-trib
    Sep 22, 2022 @ 08:31
    Corné Hoskam
    0

    Hi Martin,

    The UseHttps setting is exclusively for the backoffice to redirect to https, among a handful of other things it does. If you want to enforce Https redirecting for serving content too, you can make use of the HttpsRedirection middleware. For more details check out: https://our.umbraco.com/documentation/Reference/Security/SSL-HTTPS/

    For redirecting non-www traffic you could make use of a URL rewrite rule. The Umbraco Docs has some excellent examples on rewrite rules, including non-www redirecting!

    https://our.umbraco.com/documentation/Reference/Routing/IISRewriteRules/

    Hope this helped,

    Corné

  • Martin Rud 232 posts 902 karma points c-trib
    Sep 22, 2022 @ 09:30
    Martin Rud
    0

    Thanks - that looks right.

    But I get an error on PROD when deploying this XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <rewrite>
      <rules>
        <rule name="Redirect to www prefix" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www\." negate="true" />
                <add input="{HTTP_HOST}" pattern="^localhost(:[0-9]+)?$" negate="true" />
                <add input="{HTTP_HOST}" pattern="\.umbraco\.io$" negate="true" />
            </conditions>
            <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
        </rule>
      </rules>
    </rewrite>
    

    I haven't got access to log files so I can only see this from the "frontend":

    HTTP Error 500.30 - ASP.NET Core app failed to start
    Common solutions to this issue:
    The app failed to start
    The app started but then stopped
    The app started but threw an exception during startup
    Troubleshooting steps:
    Check the system event log for error messages
    Enable logging the application process' stdout messages
    Attach a debugger to the application process and inspect
    For more information visit: https://go.microsoft.com/fwlink/?LinkID=2028265
    

    In startup I have added:

    app.UseRewriter(new RewriteOptions().AddIISUrlRewrite(env.ContentRootFileProvider, "IISUrlRewrite.xml"));
    

    just before

    app.UseUmbraco()
    

    And this in the top

    using Microsoft.AspNetCore.Rewrite;
    
  • Justin Neville 22 posts 196 karma points c-trib
    Sep 22, 2022 @ 10:53
    Justin Neville
    0

    Where have you put your IISUrlRewrite.xml file? Is it in the root folder?

    Does it work in dev/test but not prod? Could it be a file permissions issue?

    I've used this which works for me:

        var options = new RewriteOptions();
    
        if (File.Exists("IISUrlRewrite.xml"))
        {
            using (StreamReader iisUrlRewriteStreamReader = File.OpenText("IISUrlRewrite.xml"))
            {
                options.AddIISUrlRewrite(iisUrlRewriteStreamReader);
            }
        }
    
  • Martin Rud 232 posts 902 karma points c-trib
    Sep 22, 2022 @ 11:13
    Martin Rud
    0

    Thanks - I am on the track now, I think:

    I checked if the "IISUrlRewrite.xml" was in the root on the server (outside wwwroot folder) and it was not. So webdeploy haven't deployed it.

    So I tried to remove your File.Exists check: Then the same error occured. But then putting back the File.Exists check site came up again.

    Then I uploaded the file manually. AND tried to remove the File.Exists check. And the site still worked.

    Then I tried to remove the IISUrlRewrite.xml and the site went down again.

    So I think that the conclusion can be: Now the XML file can be reached by startup.cs, but Umbraco/the site ignores the rules inside the XML file (since http isn't redirected to https, and the same goes for www/non-www)

  • Justin Neville 22 posts 196 karma points c-trib
    Sep 22, 2022 @ 12:07
    Justin Neville
    0

    Could your rule be wrong?

    Your rule is negating the www domain, so it won't run the rule.

    <add input="{HTTP_HOST}" pattern="^www\." negate="true" />
    

    My HTTPS redirect rule is:

    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="^(.*)$" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        <add input="{REQUEST_URI}" pattern="^/.well-known/" negate="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" appendQueryString="true" />
    </rule>
    

    Which works for me.

  • Martin Rud 232 posts 902 karma points c-trib
    Sep 22, 2022 @ 12:20
    Martin Rud
    0

    It doesn't work, and I have just updated the XML-file:

      <?xml version="1.0" encoding="utf-8" ?>
      <rewrite>
        <rules>
          <rule name="HTTP to HTTPS redirect" stopProcessing="true">
            <match url="^(.*)$" />
            <conditions>
              <add input="{HTTPS}" pattern="off" ignoreCase="true" />
              <add input="{REQUEST_URI}" pattern="^/.well-known/" negate="true" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" appendQueryString="true" />
          </rule>
          <rule name="Redirect to www prefix" stopProcessing="true">
              <match url=".*" />
              <conditions>
                  <add input="{HTTP_HOST}" pattern="^www\." />
                  <add input="{HTTP_HOST}" pattern="^localhost(:[0-9]+)?$" negate="true" />
              </conditions>
              <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
          </rule>
        </rules>
      </rewrite>
    

    My Startup.cs is:

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
            app.UseMiddleware<MrCustomHeaders>();
    
            var options = new RewriteOptions();
            if (File.Exists("IISUrlRewrite.xml")) 
            {
                using (StreamReader iisUrlRewriteStreamReader = File.OpenText("IISUrlRewrite.xml"))
                {
                    options.AddIISUrlRewrite(iisUrlRewriteStreamReader);
                }
            }
    
            app.UseUmbraco()
                .WithMiddleware(u =>
                {
                    u.UseBackOffice();
                    u.UseWebsite();
                })
                .WithEndpoints(u =>
                {
                    u.UseInstallerEndpoints();
                    u.UseBackOfficeEndpoints();
                    u.UseWebsiteEndpoints();
                });
        }
    
  • Justin Neville 22 posts 196 karma points c-trib
    Sep 22, 2022 @ 12:37
    Justin Neville
    0

    That's odd. Does it fail is you deliberately put a mistake in the XML file? Just to prove the file is actually being found and loaded.

  • Martin Rud 232 posts 902 karma points c-trib
    Sep 22, 2022 @ 14:23
    Martin Rud
    0

    I tried that now (adding some text outside tags):

    ...
    </rule>
    Some illegal text
    <rule name="Redirect to www prefix" stopProcessing="true">
    ...
    

    It changed nothing (the site still runs fine). Then I tried once again to remove the File.Exists check and then:

    • If file was present: Site ran fine
    • If file was deleted or renamed: Site went down

    So it seems that the file is fetched by StreamReader, but it isn't read (or isn't read as an XML file).

    Have anyone tried that or have suggestions?

  • Justin Neville 22 posts 196 karma points c-trib
    Sep 22, 2022 @ 15:41
    Justin Neville
    0

    Sorry, I may have missed a line underneath, can you try this:

    var options = new RewriteOptions();
    
    if (File.Exists("IISUrlRewrite.xml"))
    {
        using (StreamReader iisUrlRewriteStreamReader = File.OpenText("IISUrlRewrite.xml"))
       {
           options.AddIISUrlRewrite(iisUrlRewriteStreamReader);
        }
    }
    
    app.UseRewriter(options);
    
  • Martin Rud 232 posts 902 karma points c-trib
    Sep 22, 2022 @ 16:18
    Martin Rud
    0

    Cool! Thanks, that made the rewrites being processed.

    The www redirect sort of works, but it adds www to already www urls. I.e.

    www.mysite.com becomes www.www.mysite.com

    <rule name="Redirect to www prefix" stopProcessing="true">
              <match url=".*" />
              <conditions>
                  <add input="{HTTP_HOST}" pattern="^www\." />
                  <add input="{HTTP_HOST}" pattern="^localhost(:[0-9]+)?$" negate="true" />
              </conditions>
              <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
          </rule>
    

    And if I chance it to the content from https://our.umbraco.com/documentation/Reference/Routing/IISRewriteRules/:

    <rule name="Redirect to www prefix" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www\." negate="true" />
        <add input="{HTTP_HOST}" pattern="^localhost(:[0-9]+)?$" negate="true" />
        <add input="{HTTP_HOST}" pattern="\.umbraco\.io$" negate="true" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
    </rule>
    

    I get the same error as in the beginning:

    HTTP Error 500.30 - ASP.NET Core app failed to start
    
  • Justin Neville 22 posts 196 karma points c-trib
    Sep 23, 2022 @ 06:40
    Justin Neville
    0

    Hi Martin,

    This works fine for me:

    <?xml version="1.0" encoding="utf-8" ?>
    <rewrite>
      <rules>
        <rule name="Redirect to www prefix" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^www\." negate="true" />
            <add input="{HTTP_HOST}" pattern="^localhost(:[0-9]+)?$" negate="true" />
            <add input="{HTTP_HOST}" pattern="\.umbraco\.io$" negate="true" />
          </conditions>
          <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
        </rule>
      </rules>
    </rewrite>
    

    Are you able to put a try/catch around the code and log any errors?

    Your rule did not negate www - hence why it was adding www twice.

  • Martin Rud 232 posts 902 karma points c-trib
    Sep 23, 2022 @ 07:56
    Martin Rud
    0

    Thanks for the tip: Can I log to Umbraco log fra Startup.cs, and how do I do it?

  • Bo Jacobsen 593 posts 2389 karma points
    Sep 23, 2022 @ 09:44
    Bo Jacobsen
    0

    Hi Martin.

    Does your webserver use a reverse proxy?

  • Justin Neville 22 posts 196 karma points c-trib
    Sep 23, 2022 @ 14:27
    Justin Neville
    0

    Hi Martin,

    You should be able to inject a logger into your Configure method and write to the log:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
    {
      ...
      logger.LogInformation("Logging startup!");
      ...
    }
    
  • Patrick de Mooij 72 posts 622 karma points MVP 3x c-trib
    Sep 23, 2022 @ 15:24
    Patrick de Mooij
    100

    You are also able to do this without an XML:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
                    app.UseHttpsRedirection();
                    app.UseRewriter(new RewriteOptions()
                        .AddRedirectToWww());
    }
    
  • Martin Rud 232 posts 902 karma points c-trib
    Sep 24, 2022 @ 09:57
    Martin Rud
    0

    Thanks - that works for me! :)

Please Sign in or register to post replies

Write your reply to:

Draft