Copied to clipboard

Flag this post as spam?

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


  • Remko 118 posts 283 karma points
    Mar 15, 2023 @ 09:13
    Remko
    0

    How to rewrite urls and add trailing slash in dotnet core, Umbraco9+

    Just want to achieve if I visit /agenda it rewrites to /agenda/ for example.

    We used to to this by adding a rewrite rule in web.config. Nowadays we add rewrite rules by adding piece of middleware and read rewrite.config file to pass to AddIISUrlRewrite()

    This seems to work for most of our rules, but in .net core matchType="IsFile" and "IsDirectory" don't seem to be supported anymore. (See: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-3.1#when-to-use-url-rewriting-middleware : "Some of the features of the server modules don't work with ASP.NET Core projects, such as the IsFile and IsDirectory constraints of the IIS Rewrite module. In these scenarios, use the middleware instead.")

    Normally we would use this:

    <rule name="Add trailing slash in Umbraco URL" stopProcessing="true">
            <match url="(.*[^/])$" />
            <conditions>                
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{REQUEST_URI}" negate="true" pattern="^/(umbraco|DependencyHandler\.axd)" />
            </conditions>
            <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
        </rule>
    

    But because these matchTypes aren't supported anymore, it now adds a trailing slash to all file/directory paths as well, wich of course messes up the whole site.

    Unfortunately Umbraco documentation itself doesn't mention this and still says to use this matchType in Umbraco 9+ but again: this doesnt work.

    Does anyone have another solution for this?

    Thanks in advance!

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Mar 15, 2023 @ 12:36
    Marc Goodson
    101

    Hi Remko

    You can list and negate the individual file extensions, and known non front end folders and preview urls...

            <rule name="Add trailing slash" stopProcessing="true">
                  <match url="(.*[^/{2,}])$" ignoreCase="true" />
                  <conditions>
                    <add input="{REQUEST_URI}" pattern="^/umbraco/" negate="true" />
                    <add input="{REQUEST_URI}" pattern="^/api/" negate="true" />
                    <add input="{REQUEST_URI}" pattern="^/install/" negate="true" />
                    <add input="{REQUEST_URI}" pattern="^/App_Plugins/" negate="true" />
                    <add input="{REQUEST_URI}" pattern="^/media/" negate="true" />
                    <add input="{REQUEST_URI}" pattern="^/swagger/" negate="true" />
                    <add input="{REQUEST_URI}" pattern="^/([0-9]+).aspx" negate="true" />
                    <add input="{URL}" pattern="^.*\.(css|js|jpg|jpeg|png|gif|mp3|html|htm|xml|txt|pdf|svg|ico|json|webmanifest)$" negate="true" ignoreCase="true" />
                  </conditions>
                  <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
                </rule>
    

    regards

    Marc

  • Remko 118 posts 283 karma points
    Mar 23, 2023 @ 08:19
    Remko
    0

    Thanks Marc,

    This helped me a lot :-)

    {URL} didn't seem to work on the server, so I replaced that with {REQUEST_URI} as well.

    Greetings, Remko

Please Sign in or register to post replies

Write your reply to:

Draft