3

I have a website and I want to create a dynamic sitemap for it. I use Google App Engine with Python and Django.

I developed another website using PHP and the sitemap.xml was accessable because of this Rewrite Rule I wrote on the .htaccess of the Apache Server.

RewriteRule (.*)\.xml(.*) $1.php$2 [nocase]

The .xml file was generated like this:

$sql_select ="SELECT titulo, title, data_insercao FROM livros l ORDER BY titulo ASC";
$result = mysql_query($sql_select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$titulo = $row['titulo'];
$title = $row['title'];
$data = $row['data_insercao'];
$sql_comentario ="SELECT data FROM comentarios WHERE livros_title = '" . $title . "' order by data desc LIMIT 0 , 1";
$result_comentario = mysql_query($sql_comentario) or die(mysql_error());
$row_comentario = mysql_fetch_array($result_comentario);
if($row_comentario){
$data = $row_comentario['data'];
}
$pieces = explode(" ", $data);
$data = $pieces[0];
$url_product = 'http://www.sinopsedolivro.net/livro/' . $title . '.html';
echo
' <url>
<loc>'.$url_product.'</loc>
<lastmod>'.$data.'</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
';
}
Is there any option for my case (GAE + Python) so when one access www.mydomain.com/sitemap.xml, he will receive from the server a .xml file with the dynamicaly content generate myself using python?

3 Answers 3

6

This is a great article by Nick Johnson on sitemaps Sitemaps

The article describes using a post deploy method of generating the sitemap, an alternative is to generate the sitemap once in a while; say once a day using cron e.g.

`- description: daily sitemap refresh
  url: /cron_generate_sitemap
  schedule: every day 02:00
  timezone: Pacific/Auckland`

If you store the sitemap within the data store then access will be very fast which impacts your SEO rankings.

I suggest you store the sitemap in a model such as that provided by Nick Johnson e.g.

`class StaticContent(db.Model):
    """Container for statically served content.  
    The serving path for content is provided in the key name.
    """
    body = db.BlobProperty()
    content_type = db.StringProperty(required=True)
    last_modified = db.DateTimeProperty(required=True, auto_now=True)
    indexed = db.BooleanProperty(required=True, default=True)
`
1
  • 1
    Wow, what a genius idea! I wish I'd thought of that. ;) Mar 31, 2011 at 0:17
3

Sure, you can provide any response you want to any url with your application. Just map your controller to /sitemap.xml and write the code that outputs your xml in there. Don't forget to set the mime type of the response to the right value.

If you are using django, you might want to read http://docs.djangoproject.com/en/dev/topics/http/urls/

2

I faced the same problem and built a library in python to dynamically generate sitemaps and have been using it since.


pip install sitemap

Usage:

from sitemap import Url, Urlset

urlset = Urlset()
url = Url('https://www.example.com/', changefreq='weekly')

urlset.add_url(url)

# urlset.to_string()
urlset.write_xml('sitemap.xml')

More info here on, project link on github: https://github.com/cxmcc/sitemap-python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.