Copied to clipboard

Flag this post as spam?

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


  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    Feb 25, 2020 @ 15:18
    Mikael Axel Kleinwort
    0

    Url Rewrite without Microsoft's Url Rewrite module for IIS

    I am having an Umbraco 8 project where the Windows Hosting provider has IIS 10 without Microsoft's Url Rewrite module.

    I am using some simple standard rewriting rules in web.config (https redirect, HSTS), and I am looking for a way to accomplish the same rewriting without Microsoft's Url Rewrite module, but I am insecure what is the best approach.

    I am grateful for some direction on this matter.

    Here are the normal rewriting rules for Microsoft's Url Rewrite module which I want to duplicate with the help of some other solution:

    <rewrite>
          <rules>
            <!-- https rewrite -->
            <rule name="HTTP to HTTPS redirect" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
            </rule>
          </rules>
          <outboundRules>
            <!-- enable HSTS -->
            <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
              <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
              <conditions>
                <add input="{HTTPS}" pattern="on" ignoreCase="true" />
                <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
              </conditions>
              <action type="Rewrite" value="max-age=63072000; includeSubDomains; preload" />
            </rule>
          </outboundRules>
        </rewrite>
    

    Best regards! Mikael

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Feb 25, 2020 @ 15:52
    Dave Woestenborghs
    2

    Hi Michael,

    You probably need some coding for doing the http to https redirect.

    For the header you are doing with the outbound rule. This can be set in the httpProtocol tag of the web.config

    In the system.webServer tag add the following if you don't have httpProtocol tag. Otherwise update the existing one

    <httpProtocol>
            <customHeaders>
              <clear />
    
              <remove name="Strict-Transport-Security" />
              <add name="Strict-Transport-Security" value="63072000; includeSubDomains; preload" />
    
            </customHeaders>
        </httpProtocol>
    
  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    Feb 26, 2020 @ 13:21
    Mikael Axel Kleinwort
    1

    Hi Dave,

    thank you for the hint to customHeaders. I missed that one. That was easy! :-)

    As for the https redirect, I changed global.asax like this and it works:

    <%@ Application Inherits="Umbraco.Web.UmbracoApplication" Language="C#" %>
    
    <script RunAt="server">
    
        protected void Application_BeginRequest()
        {
    
            // redirect to https permanently (301) if request is not https & not local
            if (!Context.Request.IsSecureConnection
                && !Context.Request.IsLocal)
            {
                Response.RedirectPermanent(
                    Context.Request.Url.ToString().Insert(4, "s"));
            }
        }
    
    </script>
    
  • Peter Aderhold 30 posts 204 karma points
    Jul 18, 2020 @ 19:48
    Peter Aderhold
    0

    You saved my day! Important for all IONOS users (1&1), since they don't have url-rewrite enabled.

  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    Jul 19, 2020 @ 19:51
    Mikael Axel Kleinwort
    0

    I am glad it helped you. Yes I too learned this when I tried to make an IONOS (1&1) webspace work with Umbraco 8.

  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    Feb 28, 2020 @ 17:06
    Mikael Axel Kleinwort
    0

    For users of the beautiful SEO Checker package, they can make use of its built-in rewrite module like this:

    using SEOChecker.Extensions.Providers.UrlRewriteProvider;
    
    namespace My.NameSpace
    {
        /// <summary>
        /// Forces Redirect to HTTPS and adds a 301 permanent redirect response
        /// </summary>
        public class HTTPSRedirect : UrlRewriteProviderBase
        {
            public override void RewriteUrl(UrlBuilder builder, UrlRewriteConfiguration config)
            {
                if (builder.Scheme.Equals("http", StringComparison.InvariantCultureIgnoreCase))
                {
                    builder.Scheme = "https";
                }
            }
        }
    }
    

    Please note that this appSettings key in web.config

        <add key="Umbraco.Core.UseHttps" value="true" />
    

    does force https only for backoffice requests.

Please Sign in or register to post replies

Write your reply to:

Draft