Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.
Sitemaps have a wide adoption including Google, Yahoo! and Microsoft so I thought it would be a good idea to integrate this in my website. A few posts later I was tired updating this XML file manually every time I changed something in the URL scheme so why not pass the task to Ruby on Rails and build the file automatically?
First you'll need a method to collect the data. I choose for the application controller as the Sitemap doesn't really belong anywhere else. The pages controller would be a better choice if you have one that manages all your sites URL's but that's entirely up to you.
The ‘render_without_layout’ part calls the view. My view is in the views/layouts folder but again, this can be anything you want.
As defined in the Sitemap method above we need a view that renders the data in an XML file. Create the Sitemap XML template in the folder you defined above (views/layouts in my case) and call it ‘sitemap.rxml’. Now build the structure of the Sitemap:
This snippet assumes your page object has a permalink and an updated_at parameter, change these if yours looks different.
There are a few things you need to know about Sitemaps: ‘loc’ is the only required element so you can drop the ‘lastmod’, ‘changefreq’ and ‘priority’ elements if you don't have any useful data for these parameters. More in detail:
See the official Sitemap protocol definition site for a full description.
You have a automatically generated Sitemap but no way to get there. Tell Rails to call your Sitemap in the routes.rb file by adding the following mapping (change the controller if you choose a different one above):
Request your new Sitemap with http://www.example.com/sitemap.xml. You may need to restart your Rails server to enable the new route.
Almost done. The Sitemap should be working by now but how does a crawler (like the Googlebot) where to look for you Sitemap? That's where the robots.txt file is for. Every crawler should request the robots.txt file first to see what it may or may not index so this is the ideal place to advertise our Sitemap. Add the following line to the robots.txt file in you public directory and make sure to use the full URL to your Sitemap (including the domain):Sitemap: http://www.example.com/sitemap.xml
That's it for today! The next time a crawler visits you site it will find the Sitemap and index it.
This post is open source. Did you spot a mistake? Ideas for improvements? Contribute to this post via Github. Thank you!