Skip to Main Content

Using Cron Jobs to Create Dynamic XML Sitemaps

Posted on 2.14.2008

by Anthony Putignano

Could there be a hotter topic than Search Engine Optimization (SEO) these days? It seems as more people tune into its importance, the more complex the methods behind it become. Therefore, it is of the utmost importance to know concentrate on the basics. For example, providing a standard XML sitemap to search engines allows them to better understand and index your Web pages. The problem is that if you have a database-driven site with thousands of pages, it can be quite a daunting task to create a sitemap. But fear not – the process can be painlessly automated.

First, you’re going to want to create a file in “/home/yoursite/” (the directory below your Web root) named “createsitemap.php”. Inside this file, include your standard database connection instructions. Then, you will want to include the following code:

Once this is done, you can use the command “php /home/yoursite/createsitemap.php” in the shell of your server to create a file named “sitemap.xml” in your Web root directory. But what if you want to have this file automatically refreshed with no further effort on your part? Simple. For that, we need to create a cron job. From the command line, simply use “crontab -e” to open your cron job editor. Then, add the line “30 4 * * * php/home/yoursite/createsitemap.php” in order for your sitemap to be automatically refreshed daily at 4:30 am. Most modern Web hosts have a GUI for even easier cron job setups, so make sure to check your control panel.

Now that you have a sitemap file in your public Web root, you’re going to want to double check that it’s valid and submit it to Google so that they know about it. For this, simply sign up for a Google Webmaster Tools account at com/webmasters/tools/, and follow the sitemap instructions. Of course, you will want to follow similar steps with various other search engines as well.

Congratulations! You’ve successfully created a self-sustaining sitemap which will be a key part of your efforts to boost search engine rankings.

Today's Top Picks for Our Readers:
Recommended by Recommended by NetLine

Leave Your Comment

Login to Comment

Become a Member

Not already a part of our community?
Sign up to participate in the discussion. It's free and quick.

Sign Up


Leave a comment
    Load more comments
    New code