Sitemaps are a valuable tool for telling search engines about the structure of your website. Creating a sitemap and submitting it to the search engines can let Google or Bing know about the pages they have missed when they crawled your site. In addition, submitting a sitemap can speed up crawl times significantly.
In this article we will show you how to build your own sitemap which you can then submit to the various search engines.
Rails Application Setup
We are going to build a very simple blog. First we will build the blog itself, then we will add the functionality needed for the sitemap. Don’t worry, this won’t take long.
First let’s create our models. We only need one model for this example, Post. The Post model is simply an instance of a blog entry. Run the commands below to create the Post model now:
Great, now let’s create our controllers. In this example we will have two controllers. The Posts controller lists our posts and lets us view individual posts, and the Sitemap controller actually generates the sitemap. Run the commands below to create these controllers now:
Now let’s add our routes. Open up your routes file and modify it to look like the code listed below. Be sure not to overwrite line 1 of your routes file with line 1 of the example code listed:
1 2 3 4 5
Now let’s add code to our controllers. First let’s add the code necessary to list and display posts to the Posts controller. Open up your Posts controller and modify it so that it looks like the code listed below:
1 2 3 4 5 6 7 8 9
Now lets add code to our Sitemap controller. The only code we need here is code to list the posts. Open up your Sitemap controller and modify it so that it looks like the code listed below.
1 2 3 4 5 6
Now let’s create the views. First lets create the index view for the Posts controller. Open up the index view and modify it so that i looks like the code listed below.
1 2 3 4 5 6 7 8 9 10
Now let’s create the show view. Open up the show view for the Posts controller and modify it so that it looks like the code listed below.
1 2 3 4
Now it’s time for the sitemap. A sitemap is an XML file that typically consists of a number of url xml elements encapsulated by a urlset xml element. Each url typically has the 4 elements listed below.
|loc||The actual url to the page you wish to list in the sitemap.|
|changefreq||How often the page changes, can be always, hourly, daily, weekly, monthly, yearly, or never.|
|priority||How important the page is within the context of your site.|
|lastmod||The date of the last modification of this page.|
An example sitemap is listed below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
This example was generated from our application. To do this yourself, create a new view for our Sitemaps controller called index.xml.builder and add in the code listed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
The first xml.url block adds an entry for our root url. In a more complex application we would probably want to add static entries for other pages. The next thing the code does is loop through each post and create a xml url element for each.
You can preview your sitemap by starting your rails server and visiting http://localhost:3000/sitemap.xml.
So far so good, That’s it!!! See ya!!! :)