Copied to clipboard

Flag this post as spam?

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


  • Jerreck 7 posts 77 karma points
    Jul 08, 2016 @ 15:22
    Jerreck
    0

    Url Rewrite .htm to extensionless, but ignore umbraco directories

    I need to redirect all of the legacy .htm urls for my website to extensionless urls, so:

    http://www.website.com/page.htm  -->  http://www.website.com/page
    

    I created the following rule in config/UrlRewriting.config, but which fixed the above issue, but created an issue where all of the backoffice paths are returning 404:

    <add name="HtmToExtensionless"
         redirect="Domain"
         ignoreCase="true" 
         rewriteUrlParameter="IncludeQueryStringForRewrite"
         virtualUrl="http://www.website.com/(.*)(\.htm)"
         redirectMode="Permanent"
         destinationUrl="http://www.website.com/$1" />
    

    So requests like these are returning 404:

    http://www.website.com/umbraco/views/components/application/umb-navigationl?umb__rnd=7.4.2.1536555776
    

    I'm pretty sure my problem is with my regex, but I'm not sure. Anyone care to advise?

  • Jerreck 7 posts 77 karma points
    Jul 08, 2016 @ 16:58
    Jerreck
    0

    Turns out I was being a dufus. Umbraco actually requests resources that have .html extensions, and my regex was grabbing those and stripping the .htm from them. Just had to check for the beginning and end of the string, and now we're golden:

    <add name="HtmToExtensionless"
         redirect="Domain"
         ignoreCase="true" 
         rewriteUrlParameter="IncludeQueryStringForRewrite"
         virtualUrl="^http://www.website.com/(.*)(\.htm)$"
         redirectMode="Permanent"
         destinationUrl="http://www.website.com/$1" />
    
  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Jul 08, 2016 @ 17:05
    Nicholas Westby
    1

    Glad you figured it out. For future reference, you can also use a negative lookahead to ignore particular paths (e.g., Umbraco):

    <add name="HtmToExtensionless"
         redirect="Domain"
         ignoreCase="true" 
         rewriteUrlParameter="IncludeQueryStringForRewrite"
         virtualUrl="^http://www.website.com/(?!umbraco/)(.*)(\.htm)$"
         redirectMode="Permanent"
         destinationUrl="http://www.website.com/$1" />
    
  • Jerreck 7 posts 77 karma points
    Jul 09, 2016 @ 03:38
    Jerreck
    0

    Thanks for the tip Nicholas. One of these days I need to sit down and actually put some effort into memorizing regex syntax :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies