Create Sitemaps for your website in PHP


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.

In today’s tutorial, I am going to show you how to build dynamic sitemap in PHP (fetching content from the database) and style it using an xsl stylesheet. Before I dig into the code, have a look at a sample sitemap format.

The above code shows a sitemap format which is having only one url. Sitemap is nothing but an xml(eXtensible Markup Language) file with a defined format.

Here is the code which will generate the sitemap by fetching the posts from the database.

So, now you know how you can generate dynamic sitemaps for your website which are nicely styled as well.


That’s all for today. Do comeback tomorrow for more awesomeness.

  • thbt

    Having multiple lines in a row that all do “$xml .=” is not a good way to divide up a long string over multiple lines. It’s extra work for the programmer and slower to execute, and is… well, just kind of a strange way to do it. Instead do one single assignment and just use the “.” operator at the end of each line like this:

    $xml .= ‘’ .

    (There should be a tab indenting the second line of that, but this comment editor is stripping out the spaces.)

    • Akshit Sethi

      I understand it’s much faster but this tutorial is for the beginners. The sole purpose is to show them how to do stuff.

      Since you have mentioned this, I will add this suggestion to the tutorial.

      Thank you.