Create XML sitemap using Jekyll

Create XML sitemap using Jekyll

A website sitemap helps search engines like Google, Bing and others to discover and index your site’s content more efficiently. An XML sitemap, in particular, provides a structured list of URLs and metadata such as last updated date about each URL, which helps search engines understand the structure and relevance of your site.

Here is a sample for https://josefzacek.com website, the XML sitemap file can be accessed by visiting https://josefzacek.com/sitemap.xml

  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
      <loc>https://josefzacek.com/blog/close-bootstrap-mobile-menu-on-menu-item-click/</loc>
      <lastmod>2024-03-13T00:00:00+00:00</lastmod>
    </url>
    <url>
      <loc>https://josefzacek.com/blog/what-is-the-difference-between-var-let-and-const-in-javascript/</loc>
      <lastmod>2023-12-09T00:00:00+00:00</lastmod>
    </url>
    <url>
      <loc>https://josefzacek.com/blog/how-to-check-jQuery-version-in-chrome-devTools/</loc>
      <lastmod>2023-11-07T00:00:00+00:00</lastmod>
    </url>
    <url>
      <loc>https://josefzacek.com/blog/whats-the-difference-between-alert-confirm-and-prompt/</loc>
      <lastmod>2023-08-22T00:00:00+00:00</lastmod>
    </url>
    <url>
      <loc>https://josefzacek.com/portfolio/wooden-world/</loc>
      <lastmod>2024-05-23T09:14:20+00:00</lastmod>
    </url>
  </urlset>

Benefits of a Sitemap

  • Improved Indexing: Helps search engines find and index new or updated content on your site quickly.

  • SEO Advantages: Ensures that all important pages are crawled and indexed, which can improve search visibility.

  • Error Identification: By reviewing your sitemap, you can identify and fix issues such as broken links or incorrect URLs.

Jekyll code

Here is the code I use on my website to generate XML sitemap, this will produce sitemap above with all relevant info for search engines.

Create sitemap.xml file in the root directory of your project and paste in the following code.

  
  ---
  layout: null
  ---

  <?xml version="1.0" encoding="UTF-8"?>
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
 
    {% for post in site.posts %}
      {% if post.sitemap != false %}
        <url>
          <loc>{{ site.url }}{{ post.url }}</loc>
          <lastmod>{{ post.date | date_to_xmlschema }}</lastmod>
        </url>
      {% endif %}
    {% endfor %}

    {% for page in site.pages %}
      {% if page.sitemap != false %}
        <url>
          <loc>{{ site.url }}{{ page.url }}</loc>
          <lastmod>{{ site.time | date_to_xmlschema }}</lastmod>
        </url>
      {% endif %}
    {% endfor %}
  </urlset>
  

Setting up the base hostname & protocol for your site

Code above contains site.url which is translated to the base hostname & protocol for your site, therefore it is important to set this value in _config.yml file like below.

  url: "https://josefzacek.com"

Submitting a Sitemap to Search Engines

After creating a sitemap its good idea to submit it to search engines to ensure they can access it:

Google:

Use Google Search Console to submit your sitemap.

Then go to the “Sitemaps” section, enter the URL of your sitemap, and submit.

Bing:

Use Bing Webmaster Tools to submit your sitemap.

Similar to Google, find the “Sitemaps” section, enter your sitemap URL, and submit.

Conclusion

A sitemap is an essential tool for improving both search engine optimization and user experience on your website. By creating and maintaining an up-to-date sitemap, you help search engines index your content more effectively.

Domains and Web Hosting

Maxer domain and web hosting

Virtual server

Get server with Digital ocean