サーバー

サイトマップとクロールとメタタグ

VPSで作ったサイトを検索できるようにする
ある程度開発が進んだ状態でやった方がいい。
内容が薄いとSEO的に不利になるかも。

バーチャルホスト設定

ドメイン名でアクセスできるようにする。
# ① VPS にログイン
ssh -i ~/.ssh/your_key.pem youruser@YOUR_VPS_IP
# ② sites-available にファイルを追加
sudo nano /etc/nginx/sites-available/yourdomain.com
中身を以下のように記入
server {
    listen 80;
    listen [::]:80;

    server_name yourdomain.jp www.yourdomain.jp;

    # 静的ファイルがあればこの下に root を指定してもOK
    # root /home/youruser/testapp/static;

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/testapp.sock;
    }

    # Let's Encrypt 用リダイレクト(後で HTTPS 化するときに便利)
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }
}

設定を有効化する

sites-enabled にシンボリックリンクを貼る
sudo ln -s /etc/nginx/sites-available/yourdomain.jp /etc/nginx/sites-enabled/
デフォルト設定を無効化(もし default が残っているなら)
sudo rm /etc/nginx/sites-enabled/default

設定テスト&再起動

テスト
sudo nginx -t
syntax is oktest is successful と出れば問題なし
再起動
sudo systemctl reload nginx
http://yourdomain.jp/ でアクセスできることを確認

クロール許可・基本メタタグを整備

検索できるようにクロールを許可する。

ローカルでプロジェクト直下に static/robots.txt(あるいは public/robots.txt)を作成し以下を記入
User-agent: *
Allow: /
Sitemap: https://www.yourdomain.com/sitemap.xml

HTML の <head> に基本メタタグを追加

ベーステンプレート(例:templates/base.html)の <head> 部分に、最低限この3つを含める。
<!doctype html>
<html lang="ja">
  <head>
    <!-- 1. ページタイトル -->
    <title>{{ title or "てすとあぷり" }}</title>

    <!-- 2. description -->
    <meta name="description" content="{{ description or "てすとあぷりは…(サイト概要)" }}">

    <!-- 3. canonical -->
    <link rel="canonical" href="{{ request.url_root.rstrip('/') }}{{ request.path }}">

    <!-- 4. その他好みで -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="robots" content="index, follow">
  </head>
  <body>
    {% block content %}{% endblock %}
  </body>
</html>
  • {{ title }}{{ description }} を各ビューで渡して動的に変えられるとより良い
  • canonical は重複コンテンツ回避に必須

サイトマップ(任意)

サイトマップ作成を自動化するFlask-Sitemap を導入
仮想環境で
pip install Flask-Sitemap
app.pyに以下を追加
from flask_sitemap import Sitemap # 追加

app = Flask(__name__)
ext = Sitemap(app=app)  # 追加。これだけで /sitemap.xml が使えるようになる
デフォルトでは app.url_map を使うが、動的引数付きのページを登録したいときは:
例)ユーザープロフィールページを登録したい時
@ext.register_generator
def generator():
    # "user_profile" はビューの endpoint 名
    for user in User.query.all():
        yield "user_profile", {"username": user.username}
ビューの定義
Flask ルートで endpoint="user_profile" を設定しておけば、 自動で <loc>...?username=... が追加された URL も sitemap に含まれる。
テンプレートを作る
templates/sitemap.xml というファイルを用意し、以下を記入
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  {% for page in pages %}
  <url>
    <loc>{{ domain }}{{ page }}</loc>
    <lastmod>{{ lastmod }}</lastmod>
  </url>
  {% endfor %}
</urlset>

動作確認 
ターミナルで以下を入力。もしくはブラウザで直接アクセスする
curl https://www.yourdomain.com/sitemap.xml
以下のような情報が出れば成功
<?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">
    <url>
        <loc>https://www.yourdomain.com/</loc>
    </url>
</urlset>%                      

ワークフロー

1. ローカルで実装
  • static/robots.txt を追加
  • templates/base.html にメタタグを組み込み
2. 動作確認
  • flask runhttp://localhost:5000/robots.txt
  • ブラウザでソースを見て <head> のタグが出ているかチェック
3. Git へコミット → develop ブランチへプッシュ → プルリク作成
4. レビュー&マージ
5. main へマージされると自動デプロイ(deploy.yml が動いているなら)
6. 本番で再確認
  • https://www.yourdomain.com/robots.txt
  • https://www.yourdomain.com/ のソースにメタタグ

 

 

ABOUT ME
いなさく
住んでる家が崩れそうなので、建て替え費用をまかなうために 副業をがんばるサラリーマン
ブログランキング・にほんブログ村へ

COMMENT

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA