Created
November 23, 2024 17:39
-
-
Save mbuckbee/c2ef03f5c44ad9baeed40f9f7d5f878d to your computer and use it in GitHub Desktop.
Rails Sitemap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sitemaps | |
get 'sitemap.xml', to: 'sitemaps#index', defaults: { format: 'xml' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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