step‐by‐step guide to creating an XML sitemap in Umbraco 8 - FadiZahhar/umbraco8showandtell GitHub Wiki
Ensure you have:
- An Umbraco 8 project set up and running.
- Access to the project's file system and the Umbraco backoffice.
- Basic knowledge of Razor syntax and Umbraco's templating system.
- In the Umbraco backoffice, navigate to Settings > Templates.
- Create a new template named
XmlSitemap
. - Replace the default content with the following Razor code:([www.helloblake.com on Notion]1)
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using System.Xml.Linq
@{
Layout = null;
Response.ContentType = "text/xml";
var nodes = Umbraco.ContentAtRoot().DescendantsOrSelf().Where(x => x.IsVisible());
var urlset = new XElement("urlset",
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
new XAttribute(XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance") + "schemaLocation",
"http://www.sitemaps.org/schemas/sitemap/0.9 " +
"http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd")
);
foreach (var node in nodes)
{
var url = new XElement("url",
new XElement("loc", node.Url(mode: UrlMode.Absolute)),
new XElement("lastmod", node.UpdateDate.ToString("yyyy-MM-ddTHH:mm:sszzz"))
);
urlset.Add(url);
}
var sitemap = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), urlset);
@sitemap.Declaration
@sitemap
}
This code:
- Sets the response content type to XML.
- Retrieves all visible content nodes.
- Generates an XML sitemap with
<loc>
and<lastmod>
elements for each node.([Stack Overflow]2, [Swimburger]3)
- Navigate to Settings > Document Types.
- Create a new document type named
Sitemap
. - Under the Templates tab, associate it with the
XmlSitemap
template you just created. - Ensure this document type is allowed at the root level if you plan to place it at the root of your content tree.([Nicola Ayan]4, [v8.codeshare.co.uk]5)
- Navigate to Content.
- Create a new content node using the
Sitemap
document type. - Name it
sitemap
(this will result in a URL likehttps://www.yoursite.com/sitemap/
). - Publish the node.([Gist]6, [Nicola Ayan]4, [docs.umbraco.com]7)
To serve the sitemap at https://www.yoursite.com/sitemap.xml
, you can set up a URL rewrite rule.
- Open your site's
web.config
file. - Locate the
<system.webServer>
section. - Add the following rewrite rule:([Nicola Ayan]4)
<rewrite>
<rules>
<rule name="Sitemap XML" stopProcessing="true">
<match url="^sitemap\.xml$" />
<action type="Rewrite" url="/sitemap/" />
</rule>
</rules>
</rewrite>
This rule rewrites requests from /sitemap.xml
to /sitemap/
, allowing search engines to access your sitemap at the standard URL.
-
- Log in to your Google Search Console.
- Select your property.
- Navigate to Sitemaps.
- Enter
sitemap.xml
and click Submit.([Seer Interactive]8, [www.helloblake.com on Notion]1, [Nicola Ayan]4)
-
Bing Webmaster Tools:
- Log in to [Bing Webmaster Tools](https://www.bing.com/webmasters/).
- Select your site.
- Navigate to Sitemaps.
- Enter the full URL to your sitemap and submit.
-
Exclude Specific Pages: Add a boolean property (e.g.,
excludeFromSitemap
) to your document types. Modify the Razor code to filter out nodes where this property is true.([v8.codeshare.co.uk]5) -
Multilingual Support: For multilingual sites, ensure your sitemap includes alternate URLs for each language variant using the
<xhtml:link>
tag.([Umbraco]9) -
Caching: Implement caching to reduce server load, especially for large sites.
Feel free to customize the sitemap generation logic to fit your site's specific needs. If you need assistance with advanced features like handling multilingual content or integrating with SEO tools, don't hesitate to ask!