Googleウェブマスターツールのサイトマップxmlを生成する

シェルとPerlで。スピード重視で手抜きでももっとましなの作りたいのだけれど。

元データ作成

シェルで

$ find /nantoka/nantoka/public_html/ -name *.html >nantoka.txt
  • こんなのがでてくる(nantoka.txt)
/index.html
/profile.html
/news/index.html
/contact/index.thml

ウェブのルートディレクトリまでパスを指定すれば、あとはhttp://〜をテキストエディタの置換機能とかで行頭に足してあげればできあがり。あえてここで足す必要はないのですが。

  • 足してあげる(nantoka.txt)
http://www.〜.co.jp/index.html
http://www.〜.co.jp/profile.html
http://www.〜.co.jp/news/index.html
http://www.〜.co.jp/contact/index.thml

動的ファイルは別途、パラメタ部分をDBから引っ張ってきて、URLを生成できるスクリプトを別に書いています。

出したくない行を削除などして整形したら、PerlXMLファイルを生成

シェルで

$ perl sitemap.pl >nantoka.xml
  • プログラム(sitemap.pl)
#!/usr/bin/perl
use strict;
use warnings;

#ファイル名を指定
open (IN,"nantoka.txt") or die "error";

print <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
EOF

foreach(<IN>){

chomp;

#日付を指定
print <<EOF;
<url>
<loc>$_</loc>
<lastmod>2007-12-13</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>

EOF

}

print <<EOF;
</urlset>
EOF