Skip to content

Instantly share code, notes, and snippets.

@dmitry
Forked from anonymous/nginx-site.conf
Last active March 8, 2023 15:53
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dmitry/4341237 to your computer and use it in GitHub Desktop.
Shows how to generate sitemap with sitemap_generator gem for multilingual site and set rewrite urls in nginx conf.
rewrite ^/sitemap1.xml.gz /sitemaps/$host/sitemap1.xml.gz last;
rewrite ^/sitemap_index.xml.gz /sitemaps/$host/sitemap_index.xml.gz last;
# or
location /sitemap {
# it can be already switched on for globally
# gzip_static on;
rewrite ^/sitemap.xml /sitemaps/$host/sitemap.xml last; # that's will catch .gz files and served as normal static file, but with gzip header
}
require 'rubygems'
require 'sitemap_generator'
# Constants::HOSTS = {'www.site.com' => :en, 'www.site.de' => :de}
Constants::HOSTS.each do |host, locale|
SitemapGenerator::Sitemap.default_host = "http://#{host}"
SitemapGenerator::Sitemap.public_path = "public/sitemaps/#{host}"
SitemapGenerator::Sitemap.sitemaps_path = ''
SitemapGenerator::Sitemap.create do
add root_path, priority: 1
Property.find_each do |property|
add property_path(property),
priority: 0.8,
lastmod: (property.updated_at || property.created_at),
alternates: Constants::HOSTS.map { |h, l|
if locale != l
{
href: property_url(property, host: h),
lang: l
}
end
}.compact
end
end
SitemapGenerator::Sitemap.ping_search_engines
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment