Skip to content

Instantly share code, notes, and snippets.

@mbuckbee
Created November 23, 2024 17:39
Show Gist options
  • Save mbuckbee/c2ef03f5c44ad9baeed40f9f7d5f878d to your computer and use it in GitHub Desktop.
Save mbuckbee/c2ef03f5c44ad9baeed40f9f7d5f878d to your computer and use it in GitHub Desktop.
Rails Sitemap
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<% @pages.each do |path, timestamp| %>
<url>
<loc>https://knowatoa.com<%= path %></loc>
<lastmod><%= timestamp.strftime("%Y-%m-%d") %></lastmod>
</url>
<% end %>
</urlset>
# Sitemaps
get 'sitemap.xml', to: 'sitemaps#index', defaults: { format: 'xml' }
class SitemapsController < ApplicationController
# Creates a sitemap index file referring to the other sitemaps
def index
@sitemaps = []
# MARKETING PAGES
# Get from the pages view folder
dir_path = "app/views/pages/"
most_recent_file = Dir.glob(File.join(dir_path, '*')).max_by { |f| File.mtime(f) }
# Overall sitemap last modified date
@last_modified = File.mtime(most_recent_file)
@pages = {}
# Marketing Pages
file_paths = [
'/',
'/contact',
'/tos',
'/privacy-policy',
'/about',
'/pricing'
]
# Platform Firewall Pages
file_paths.each do |page_path|
@pages[page_path] = @last_modified
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment