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.
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);
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)
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?
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.
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:
How can I achieve the same thing in Umbraco 9 and .Net 5?
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
Thanks for the help Benjamin.
I changed the startup.cs to look like this:
I also added a IISUrlRewrite.xml file to the project root (not wwwroot) with the following:
When I hit /sitemap.xml I get a page displaying "Status Code: 404; Not Found".
How can I get this to work?
I think you were very close on this. I think it might be due to the default "alttemplate" settings for the project.
Making this change should bode better for you
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.
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:
And still get the 404 error, whereas in the previous versions via IIS, it would rewrite.
Is there anything I'm missing?
Thanks
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 :-/
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)
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?
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.
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
and I will add a robots.txt which will look like this one
So, Do I need my sitemap to be called "sitemap.xml" ???
Did anyone figure this out?
I've got the below, but get a
Status Code: 404; Not Found
if I visit https://localhost:44387/sitemap.xml or any other items.Hi got that working for the robots.txt with the @Jannik Anker answer
I have a doctype "Robots TXT" and a node with the same name. The doctype has only one property a text area "Crawl instructions".
I have a multi lang website with the domains looking like that
{domainName}
{domainName}/el
{domainName}/en
Startup.cs
RobotsTxtController
RobotsTXT.cshtml
So, If you visit the URL which is provided by Umbraco for the my node "Robots TXT" (ex: https://{domainName}/robotstxt) the page will not work.
But if you browse
https://{domainName}/robots.txt
https://{domainName}/el/robots.txt
https://{domainName}/en/robots.txt
you get the appropriate result
The sitemap-xml is a regular page/node but in the response headers has the "Content-Type", "text/xml".
I guess you could do the same (or something like that) for the sitemap as well.
Hope that helps!
The correct answer for me was changing the order of the middleware suggested by @Andy Hale.
Move your
app.UseRewriter(options)
section above theapp.UseUmbraco()
section.If you're running the site in IIS then you can use the same method for V9 - I just added a simplified web.config to the root of the 'publish' folder:
is working on a reply...