Create a Google Sitemap for your website

A Google Sitemap is a very important element for ranking of your website : it is used to tell Google which pages it must include in his engine. It also helps your new pages to be ranked quicker, and to be sure that all your relevant content is indexed.

A Sitemap, it's basically a map of your website (I hope you already guessed it); a tree of pages in which the level of importance of certain pages over some other pages is cleary visible.

Of course, it's not mandatory to create a Sitemap for your website, its purpose is especially to help Google (then also yourself). It is possible that not all the pages you included in your Sitemap will be added in Google. Even if you show him the way, Google isn't dumb, it will only rank pages that are really relevant.

This means that you should give up for contact pages, help pages, or any other page that is not relevant. Rather than this, try to rank your « best » pages, those that have content of quality.

Depending on how your website is build (static website or dynamic website), the way to build your sitemap may vary. This article will show you some common ways to do it.

At any time, you should refer to the Google documentation about Sitemaps.

Create a static Sitemap

When I say static, I mean a Sitemap you have make yourself, line by line, and that won't update itself automatically. That's what we normally use for websites that only have static contents, pure HTML.

To do this, don't waste your time in writing it yourself, you can rather use an automatic Sitemap generator. You can also use it for a dynamic website that doesn't rewrite URLs, but you will have to generate it everytime you want to update it (usally, often) – this is something you'll forget to do, for sure.

Create a dynamic Sitemap

A dynamic Sitemap will update itself automatically, which is useful if you update your website on a regular basis. The method given here is using URL Rewriting to make it more clean and simple.

First, create a .htaccess file, located at the root, or modify the .htaccess file you may already have :

  1. Options +FollowSymlinks
  2. RewriteEngine on
  3.  
  4. # Link for the Sitemap
  5. RewriteRule ^sitemap\.xml$  /sitemap.php [L,NC]

This way, sitemap.php will allow us to automatically generate the content of the XML file (the real Sitemap). Each time Google will try to open sitemap.xml, sitemap.php will be executed and will make an update of the XML « file ».

Create a file named sitemap.php. In the example bellow, we get the content of articles of an imaginary blog.

The structure of the table used in the example bellow :

  1. CREATE TABLE IF NOT EXISTS `blog` (
  2. `id` mediumint(9) NOT NULL COMMENT 'ID of the article',
  3. `texte` text NOT NULL COMMENT 'Text of the article',
  4. `date` date NOT NULL
  5. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Articles of blog';

The content of the file sitemap.php :

  1. // MySQL Connection$host = "localhost";
  2. // normally, it's localhost
  3. $user = "USERNAME";
  4. $pass = "PASSWORD";
  5. $bdd = "NOM_OF_THE_DB";
  6.  
  7. @mysql_connect($host,$user,$pass) or die("Unable to connect : ".mysql_error());
  8. @mysql_select_db("$bdd") or die("Unable to select dabatase : ".mysql_error());
  9.  
  10. // Indicate file type : XML document
  11. header("Content-Type: text/xml");
  12.  
  13. // MySQL query to get articles of the blog
  14. $requete = "SELECT id, date FROM blog ORDER BY date DESC";
  15. $resultat = mysql_query($requete);
  16.  
  17. // Writing of the XML file
  18. // Headers :
  19. $sm = '';
  20. $sm .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21. xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
  22. http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">';
  23.  
  24. // Articles of the blog
  25. while ($blog=mysql_fetch_array($resultat,MYSQL_ASSOC)) {
  26. $sm .= '<url>';
  27. $sm .= '<loc>http://yoursite.com/blog?id='.$blog['id'].'</loc>'; // you may adapt the URL syntax to your need, this is an example
  28. $sm .= '<lastmod>'.$blog['date'].'</lastmod>';
  29. $sm .= '<changefreq>monthly</changefreq>';
  30. $sm .= '<priority>0.7</priority>';
  31. $sm .= '</url>';
  32. }
  33.  
  34. $sm .= '';
  35.  
  36. echo $sm;

As you may guess, the Sitemap schema is working on this structure :

  1. <url>
  2. <loc>
  3. <lastmod>
  4. <changefreq>
  5. <priority>
  6. </url>

<loc> Location of the page. Don't forget to write the domain (http://www.domain.com).
<lastmod> When was the page updated the last time (or when was it created). This data is normally recorded in your database by your scripts. You must give this value with a W3C valid format.
<changefreq> What is the frequency of updates for this page? (always, hourly, daily, weekly, monthly, yearly, never). This is only to help Google, Google may not respect this indication. This is useless to set always if you do not plan to update a page once it is created.
<priority>

Priority is a way for Google to know which pages are more important than some others in your website. This comparison doesn't apply to other websites. It is useless to put 1 everywhere, you won't help yourself. Give a higher priority to pages that seem more important and that updates often.

If everything is well working, you should be able to see your Sitemap if you try to reach the page sitemap.xml. Check if it is valid, and then submit it to Google.

Bien sûr, il est possible d'améliorer ce script de plusieurs façons. Par exemple, vous pouvez stocker dans vos tables de données, dans une colonne priority, la priorité à donner à chacune des pages. Même chose pour le changefreq.

Of course, it's possible to upgrade this script in many ways. For instance, you also could store your pages priorities in an additional column. Same thing for changefreq.

It's of course a lot less complicated with CMS.

Create a dynamic Sitemap for Joomla!

For Joomla!, you have 2 major choices of components :  xMap and Joomap, both available for J!1.0x et J!1.5x versions.

Je n'ai pas essayé Joomap, mais xMap fonctionne très bien. Il existe des extensions pour la plupart des composants connus (Sobi2, Adsmanager, Community Builder, ...), ce qui est indispensable. Il s'agit des deux plus connus, mais il existe d'autres composants.

I didn't try Joomap, but xMap works fine. There are extensions for most of the popular components (Sobi2, Adsmanager, Community Builder, ...), which is very important. xMap and Joomap are the most known components, but there are other components you may want to try.

Create a dynamic Sitemap for Drupal

Install the XML Sitemap module for Drupal, disponible pour Drupal 5.x.

Créer un sitemap dynamique pour Wordpress

Download the plugin of Arne Brachhold, which works fine. If you are interested into SEO, I invite you to read my article about SEO Ranking with Wordpress.

Comments

Hi, good post. I have been

Hi, good post. I have been woondering about this issue,so thanks for posting. I’ll definitely be coming back to your site.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <actionscript>, <apache>, <bash>, <dos>, <html>, <javascript>, <mysql>, <php>, <xml>.
  • Every instance heading tags will be modified to include an id attribute for anchor linking.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Image links with 'rel="lightshow"' in the <a> tag will appear in a Lightbox slideshow when clicked on.
  • Links to HTML content with 'rel="lightframe"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to video content with 'rel="lightvideo"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to inline or modal content with 'rel="lightmodal"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • Insert <!--tableofcontents [list: ol; title: Table of Contents; minlevel: 2; maxlevel: 3; attachments: yes;]--> to insert a mediawiki style collapsible table of contents. Arguments within [] are optional.

More information about formatting options