この記事の対象読者
- サイトのクロールとインデックスを最適化したい人
- サイトマップや robots.txt の書き方を知りたい人
- Django でサイトマップを自動生成したい人
XMLサイトマップとは
XML サイトマップは、サイト内の 全ページの URL を一覧にした XML ファイル です。Google のクローラ(Googlebot)がこのファイルを読むことで、サイト内のページを効率的に発見・巡回できます。
サイトマップが特に重要なケース
- ページ数が多い大規模サイト
- 新しいサイトで外部リンクが少ない
- ページ間のリンクが少ない
- 頻繁にコンテンツを更新する
サイトマップの書き方
sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/</loc>
<lastmod>2026-02-20</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://example.com/article/python/variables/</loc>
<lastmod>2026-02-15</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
</urlset>
各タグの意味
| タグ | 意味 | 必須 |
|---|---|---|
<loc> | ページの URL | 必須 |
<lastmod> | 最終更新日 | 推奨 |
<changefreq> | 更新頻度の目安 | 任意 |
<priority> | 優先度(0.0〜1.0) | 任意 |
Django でサイトマップを自動生成
Step 1: INSTALLED_APPS に追加
config/settings.py
INSTALLED_APPS = [
...
'django.contrib.sitemaps',
...
]
Step 2: Sitemap クラスの作成
blog/sitemaps.py
from django.contrib.sitemaps import Sitemap
from .models import Article, Classification
class ArticleSitemap(Sitemap):
changefreq = "weekly"
priority = 0.8
protocol = "https"
def items(self):
return Article.objects.filter(is_published=True)
def lastmod(self, obj):
return obj.updated_at
def location(self, obj):
return obj.get_absolute_url()
class CategorySitemap(Sitemap):
changefreq = "weekly"
priority = 0.6
protocol = "https"
def items(self):
return Classification.objects.all()
def location(self, obj):
return f"/category/{obj.get_slug_path()}/"
Step 3: URL の登録
config/urls.py
from django.contrib.sitemaps.views import sitemap
from blog.sitemaps import ArticleSitemap, CategorySitemap
sitemaps = {
"articles": ArticleSitemap,
"categories": CategorySitemap,
}
urlpatterns = [
path('sitemap.xml', sitemap, {'sitemaps': sitemaps}),
...
]
これで /sitemap.xml にアクセスすると、全公開記事とカテゴリページの URL が自動生成されます。
robots.txt とは
robots.txt は、検索エンジンのクローラに対して クロールして良いページとダメなページ を指示するファイルです。サイトのルートディレクトリに配置します。
robots.txt の書き方
robots.txt
User-agent: *
Disallow: /admin/
Disallow: /manage/
Disallow: /search/
Disallow: /login/
Disallow: /logout/
Sitemap: https://example.com/sitemap.xml
各ディレクティブの意味
| ディレクティブ | 意味 |
|---|---|
User-agent: * | 全てのクローラに対する指示 |
Disallow: /admin/ | /admin/ 以下のクロールを禁止 |
Sitemap: | サイトマップの URL を通知 |
robots.txt の注意点
Disallowはクロールの禁止であり、インデックスの禁止ではない- インデックスを確実に防ぐには
<meta name="robots" content="noindex">を使う - 機密情報の URL を
Disallowに書くと、逆にその存在を公開してしまう
Django での動的生成
config/urls.py
from django.http import HttpResponse
def robots_txt(request):
lines = [
"User-agent: *",
"Disallow: /admin/",
"Disallow: /manage/",
"Disallow: /search/",
"Disallow: /login/",
f"Sitemap: https://{request.get_host()}/sitemap.xml",
]
return HttpResponse("
".join(lines), content_type="text/plain")
urlpatterns = [
path('robots.txt', robots_txt),
...
]
RSS フィードの設定
RSS フィードを設定すると、Google に新着記事を素早く通知できます。また、RSS リーダー経由のアクセスも獲得できます。
Django での RSS フィード生成
blog/feeds.py
from django.contrib.syndication.views import Feed
from .models import Article
class LatestArticlesFeed(Feed):
title = "サイト名"
description = "サイトの説明"
link = "/"
def items(self):
return Article.objects.filter(
is_published=True
).order_by("-published_at")[:20]
def item_title(self, item):
return item.title
def item_description(self, item):
return item.excerpt
def item_link(self, item):
return item.get_absolute_url()
HTML の <head> にフィードのリンクを追加します。
HTML - head内
<link rel="alternate" type="application/rss+xml" title="サイト名" href="/feed/">
まとめ
- XMLサイトマップ: 全ページの URL を Google に通知。Search Console でサイトマップを送信
- robots.txt: 不要なページのクロールを制御。Sitemap ディレクティブでサイトマップの場所を通知
- RSS フィード: 新着記事を素早く配信。Google のインデックス促進にも効果あり
- Django では
django.contrib.sitemapsとFeedクラスで自動生成できる