Google XML Sitemaps 4.0.x による sitemap.xml 生成の問題

Ver. 4.0 以降の Google XML Sitemaps の挙動、主に、sitemap.xml, robots.txt について、少しまとめておく。

Google XML Sitemaps は自動で sitemap.xml や sitemap.xml.gz を「生成」してくれるツールとしてよく利用されていた。実際に sitemap.xml というファイルを、index.php と同じ階層にファイルとして生成してくれていた。

ところが、Google (XML) Sitemap Generator Plugin Changelog によれば、

No static files anymore, sitemap is created on the fly!

とのことで、sitemap.xml は必要となった時にその度に計算されて、作られるようである。

nginx を httpd に使っているが、あるサイトではうまくファイルが表示されなかった。そのサイトでは、permalink を用いず、存在しないファイルへのリンクは全て 404 を投げるようにしていた。sitemap.xml へのアクセスを index.php にそのまま rewrite してみても、求めていた表示は得られなかった。

Apache を利用しているあるサイトは、permalink を用いていたためか、または、.htaccess に「ファイルが見当たらなければ index.php に処理を投げる」という設定にしたためか、sitemap.xml へのアクセスで問題なく表示された。

解決方法は
Google (XML) Sitemap Generator Plugin Changelog の support にあった。
https://wordpress.org/support/topic/404-file-not-found-see-here
https://wordpress.org/support/topic/40-is-broken-in-fpmfastcgi-servers
を参考に、
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$ "/index.php?xml_sitemap=params=$2;zip=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html$ "/index.php?xml_sitemap=params=$2;html=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$ "/index.php?xml_sitemap=params=$2;html=true;zip=true" last;

を nginx の設定に書き加えたところ、うまく動作するようになった。nginx で rewrite するときに index.php?xml_sitemap=params=~ を利用すれば良いようであった。

また、robots.txt については、

The sitemap is generated by WordPress, so make sure there is no robots.txt file saved in your blog directory!

とある…robots.txt をそのまま index.php にリダイレクトや、rewrite してもだめである。困ったものだ。rewrite ^/robots\.txt$ "/index.php?robots=1" last;としておけばよいようだ。

参考にしてほしい。

misc

Posted by tako