Copied to clipboard

Flag this post as spam?

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


  • Brendan Rice 487 posts 848 karma points
    Jun 21, 2021 @ 13:53
    Brendan Rice
    0

    URL Rewrite in Umbraco 9

    How should URL rewriting be done in Umbraco 9?

    This used to be done using the Web.Config but it seems like that's been replaced with appsettings.json.

    What I'm trying to do is put a rewrite rule in for the xmlsitemap. It would have looked like this in the web.config:

    <rule name="SiteMap" patternSyntax="Wildcard" stopProcessing="true">
      <match url="sitemap.xml" />
      <action type="Rewrite" url="xml-sitemap" appendQueryString="false" />
    </rule>
    

    How can I achieve the same thing in Umbraco 9 and .Net 5?

  • Benjamin Carleski 30 posts 247 karma points MVP c-trib
    Jun 21, 2021 @ 14:28
    Benjamin Carleski
    1

    The URL Rewriting extension in IIS has been replaced with the URL Rewriting Middleware for ASP.NET Core. You can find documentation at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting

  • Brendan Rice 487 posts 848 karma points
    Jun 21, 2021 @ 16:01
    Brendan Rice
    0

    Thanks for the help Benjamin.

    I changed the startup.cs to look like this:

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
            app.UseUmbraco()
                .WithMiddleware(u =>
                {
                    u.WithBackOffice();
                    u.WithWebsite();
                })
                .WithEndpoints(u =>
                {
                    u.UseInstallerEndpoints();
                    u.UseBackOfficeEndpoints();
                    u.UseWebsiteEndpoints();
                });
    
            ApplyRewriteRules(app);
        }
    
        private static void ApplyRewriteRules(IApplicationBuilder app)
        {
            using (StreamReader iisUrlRewriteStreamReader =
                File.OpenText("IISUrlRewrite.xml"))
            {
                var options = new RewriteOptions()
                    .AddIISUrlRewrite(iisUrlRewriteStreamReader);
    
                app.UseRewriter(options);
            }
    
            app.UseStaticFiles();
        }
    

    I also added a IISUrlRewrite.xml file to the project root (not wwwroot) with the following:

    <rewrite>
      <rules>
        <rule name="Sitemap.xml rewrite" stopProcessing="true">
          <match url="^sitemap.xml$" />
          <action type="Rewrite" url="/xml-sitemap" appendQueryString="false" />
        </rule>
      </rules>
    </rewrite>
    

    When I hit /sitemap.xml I get a page displaying "Status Code: 404; Not Found".

    How can I get this to work?

  • Søren Kottal 563 posts 3696 karma points MVP 3x c-trib
    Jul 25, 2021 @ 18:41
    Søren Kottal
    1

    Hi Brendan

    Are you running the site in IIS or through Kestrel? If through Kestrel, you can't use IIS Url rewrites AFAIK. You have to implement redirects/rewrites through code in the middleware.

  • Javz 35 posts 137 karma points
    Aug 10, 2021 @ 22:56
    Javz
    0

    Hi all,

    Rather than creating another thread, I'll add to this - I'm actually facing a similar issue with V9 URL Rewrites.

    I've followed this - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-5.0, minus the xml and txt files but the URL's are still not rewriting.

    I've done the following:

            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
            app.UseUmbraco()
                .WithMiddleware(u =>
                {
                    u.WithBackOffice();
                    u.WithWebsite();
                })
                .WithEndpoints(u =>
                {
                    u.UseInstallerEndpoints();
                    u.UseBackOfficeEndpoints();
                    u.UseWebsiteEndpoints();
                });
    
            // URL Rewrites
            var rewrite = new RewriteOptions()
                .AddRewrite("^sitemap-xml", "/sitemap.xml", true)
                .AddRewrite("^sitemapxml", "/sitemap.xml", true);
            app.UseRewriter(rewrite);
            app.UseRouting();
            app.UseStaticFiles();
        }
    

    And still get the 404 error, whereas in the previous versions via IIS, it would rewrite.

    Is there anything I'm missing?

    Thanks

  • Jannik Anker 42 posts 230 karma points c-trib
    Aug 17, 2021 @ 09:55
    Jannik Anker
    1

    Just making sure. You do have a sitemap.xml file, right? :-) And is it located in the wwwroot folder as it should be?

    Here's what I have - and it's working. I'm pointing towards a custom controller instead of a physical file, though, but it looks pretty similar to your code :-/

             var options = new RewriteOptions()
                    .AddRewrite(@"^sitemap.xml$", 
                       "/umbraco/surface/sitemap/content",
                       skipRemainingRules: true);
    
            app.UseRewriter(options);
    
  • Javz 35 posts 137 karma points
    Aug 20, 2021 @ 17:47
    Javz
    0

    I actually don't have a physical xml file within the root, however it is generated via Umbraco and is called sitemap.xml (had this working on Umbraco V8 with the same approach)

  • Andy Hale 5 posts 77 karma points
    Aug 21, 2021 @ 11:42
    Andy Hale
    0

    Hi Javz :)

    The order that the middleware is added in the configuration method is also the order that they'll be processed for requests as responses. It's like a pipeline, so the requests pass through the pipe from the top of what we've declared down to the bottom.

    At any point along the pipeline, middleware can choose to return without passing on to the next point in the chain. For example, with authentication, if someone isn't authenticated, the authentication middleware can just terminate the pipeline and return with an error message because calling the rest of the middleware would be pointless. Microsoft have some diagrams in the documentation showing how requests pass down through each layer of middleware and then the response comes back up through the middleware from the bottom to the top.

    I'm sill playing with Umbraco 9 but I wonder if its because the Umbraco middleware is not finding a page in the CMS for the sitemap, returning a 404 and terminating so that the rest of the middleware isn't processed?

    What happens if you move the rewrite higher up the pipeline like this?

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
    
        // URL Rewrites
        var rewrite = new RewriteOptions()
            .AddRewrite("^sitemap-xml", "/sitemap.xml", true)
            .AddRewrite("^sitemapxml", "/sitemap.xml", true);
        app.UseRewriter(rewrite);
    
        app.UseStaticFiles();
    
        app.UseUmbraco()
           .WithMiddleware(u =>
           {
               u.WithBackOffice();
               u.WithWebsite();
           })
           .WithEndpoints(u =>
           {
               u.UseInstallerEndpoints();
               u.UseBackOfficeEndpoints();
               u.UseWebsiteEndpoints();
           });
         }
    

    The idea that being when the request comes in, the rewrite engine middleware processes the request first, realises that it's got a matching rule and then rewrites the request before we get to Umbraco.

  • Thomas Kassos 43 posts 227 karma points
    15 days ago
    Thomas Kassos
    0

    I am facing the same issue.

    I would like to ask, does it matter if the xml page is called sitemap-xml and not sitemap.xml?

    In the view I am generating the sitemap I am adding this value to the header Content-Type

    @{
        Layout = null;
        Context.Response.Headers.Remove("Content-Type");
        Context.Response.Headers.Add("Content-Type", "text/xml");
    }  
    

    and I will add a robots.txt which will look like this one

    User-agent: *
    Disallow: /bin/
    Disallow: /config/
    Disallow: /install/
    Disallow: /umbraco/
    Disallow: /views/
    
    Sitemap: https://{domain}/sitemap-xml/
    

    So, Do I need my sitemap to be called "sitemap.xml" ???

Please Sign in or register to post replies

Write your reply to:

Draft