Tag Archives: XML

Dynamic RSS feeds

RSS feeds became essential for blogs and any other website that publishes content on a regulary basis. It’s a good way to broadcast your content and allow your visitors to follow your blog by suscribing to your feeds.

It allows also to submit your RSS feeds in RSS directory, which will give you deep links (other links than your homepage), in addition to bring new visitors from the directory because you show them interesting content before they have access to your website.

Most of CMS (as Joomla, WordPress, Dotclear, etc.) already have an embeded script that’ll generate RSS feeds for you. But if you own a dynamic website, scripted « by hand », you’ll have to do it by yourself…

Let’s say that you own a dynamic PHP website that provides articles from time to time. We’ll create a PHP function to extract articles from the database, then to write them in a XML file, which will be your RSS feed. This way, your RSS reed will update itself automatically.

GENERATE RSS FEED AUTOMATICALLY WITH URL REWRITING

To do it, we’ll use URL Rewriting, because it allows to do all that in a much cleaner way, by simulate the RSS file on demand, instead of constantly editing it in a real RSS file.

Create a .htaccess that you will place to root of your website, or edit the .htaccess file you may already own :


Options +FollowSymlinks
RewriteEngine on
# Link for RSS feed
RewriteRule ^rss\.xml$ /rss.php [L,NC]

With that rewriting, rss.php file will generate automatically the XML content of the RSS feed; it’ll simulate the file.

Create a file named rss.php. We extract, in the following example, last articles from an imaginary blog.

Structure of the table used in the example :


CREATE TABLE IF NOT EXISTS `blog` (
`id` mediumint(9) NOT NULL COMMENT ‘ID of the article’,
`texte` text NOT NULL COMMENT ‘Text of the article’,
`date` date NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT=’Articles of the blog’;

To insert into rss.php :

// MySQL connexion
$host = “localhost”; // normally, localhost
$user = “USERNAME”;
$pass = “PASSWORD”;
$bdd = “DB_NAME”;

@mysql_connect($host,$user,$pass) or die(“Connexion error: “.mysql_error());
@mysql_select_db(“$bdd”) or die(“Database selection error: “.mysql_error());

// Specify content type : XML
header(“Content-Type: text/xml”);

// MySQL query that extracts content from DB
$requete = “SELECT id, date FROM blog ORDER BY date DESC LIMIT 10″; // last 10 articles
$resultat = mysql_query($requete);

// XML writing
// header :
$sm = ”;
$sm .= ‘xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.84/sitemap.xsd”>’;

// Articles of the blog
while ($blog=mysql_fetch_array($resultat,MYSQL_ASSOC)) {
$xml = ‘‘;
$xml .= ‘‘;
$xml .= ‘RSS FEED TITLE‘; // Title of your RSS feed. For instance : Last articles of blog xyz
$xml .= ‘HTTP://WWW.YOURSITE.COM/‘; // Your website’s URL
$xml .= ‘‘.$title.’‘; // Your website’s description

$xml .= ‘‘;
$xml .= ‘YOUR_SITE/IMAGE.GIF‘; // An image, logo
$xml .= ‘IMAGE TITLE‘; // Normally the same thing as the title before
$xml .= ‘HTTP://WWW.YOURSITE.COM/‘; // your website’s URL
$xml .= ‘DESCRIPTION‘; // Description of you website (for image)
$xml .= ‘‘;
}

$i = 0;

foreach($news as $a) {

if($i < 12) { $id = $a['date']; $datexml = explode('-', $a['date']); $date2=date("D, d M Y H:i:s", mktime(0,0,0, $datexml[1],$datexml[2],$datexml[0])); $lien = "http://www.yoursite.com/articles.php?id=" . $id; // Link to your article (modify as your wish) $xml .= '‘;
$xml .= ‘‘.$a[‘titre’].’‘; // Article title
$xml .= ‘‘.$lien.’‘; // Link to article
$xml .= ‘‘.$lien.’‘; // Permalink
$xml .= ‘‘.$date2.’ GMT‘;
$xml .= ‘‘.strip_tags($a[‘txt’]).’‘; // Text of the article, removing HTML tags that it may contain
$xml .= ‘
‘;

$i++;
}
}

// end of XML
$xml .= ‘‘;
$xml .= ‘‘;
echo $xml;

Then, when you will try to reach http://www.votresite.com/rss.xml, rss.php will be called an a XML file will be generated and shown, with 10 last articles of your blog.

If you wish, it’s also possible to make a function with this code, and instead of simulating the RSS file with URL Rewriting, you can directly write into a rss.xml file. All you have to do is write something like this, to write in a real file :

// write file
$fp = fopen(“flux.xml”, ‘w+’);
fputs($fp, $xml);
fclose($fp);

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 :


Options +FollowSymlinks
RewriteEngine on

# Link for the Sitemap
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 :


CREATE TABLE IF NOT EXISTS `blog` (
`id` mediumint(9) NOT NULL COMMENT ‘ID of the article’,
`texte` text NOT NULL COMMENT ‘Text of the article’,
`date` date NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT=’Articles of blog’;

The content of the file sitemap.php :

// MySQL Connection$host = “localhost”;
// normally, it’s localhost
$user = “USERNAME”;
$pass = “PASSWORD”;
$bdd = “NOM_OF_THE_DB”;

@mysql_connect($host,$user,$pass) or die(“Unable to connect : “.mysql_error());
@mysql_select_db(“$bdd”) or die(“Unable to select dabatase : “.mysql_error());

// Indicate file type : XML document
header(“Content-Type: text/xml”);

// MySQL query to get articles of the blog
$requete = “SELECT id, date FROM blog ORDER BY date DESC”;
$resultat = mysql_query($requete);

// Writing of the XML file
// Headers :
$sm = ”;
$sm .= ‘xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.84/sitemap.xsd”>’;

// Articles of the blog
while ($blog=mysql_fetch_array($resultat,MYSQL_ASSOC)) {
$sm .= ‘‘;
$sm .= ‘http://yoursite.com/blog?id=’.$blog[‘id’].’‘; // you may adapt the URL syntax to your need, this is an example
$sm .= ‘‘.$blog[‘date’].’‘;
$sm .= ‘monthly‘;
$sm .= ‘0.7‘;
$sm .= ‘
‘;
}

$sm .= ”;

echo $sm;

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






<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.