KotoriForBlog

【Remix】robots.txt実装ガイド

作成日: 10/27/2024, 7:10:44 AM

この記事では**fRemix**を用いた**fRobots.txt**の作り方をまとめていきます。 remixではリソースルートを用いることで簡単に作ることができます。SEOを意識する人におすすめです。

【remix】リソースルートの使い方

remixには**fルーティング**の一つにリソースルートが存在します。リソースルートでは**fJSON、テキストファイル、画像の提供等**を行うことができます。 リソースルートの命名法は**f特殊**です。

  • sitemap[.]xml.ts // sitemap.xml
  • data[.]json.ts // data.json
  • style[.]css.ts // style.css

上記のようにすることで、ファイルの提供が可能です。

【remix】robots.txtの書き方

robots.txtではクローラーの種類ごとに、訪問すべきでないページを指定することができます。 有名なサイトのrobots.txtを見て、参考にするのをおすすめします。 **lf[googleのrobots.txt](https://www.google.com/robots.txt)** **lf[yahooのrobots.txt](https://www.yahoo.co.jp/robots.txt)**

エラー: 不明なセクションタイプ

このセクションは正しく表示できません。

セクションタイプ: section

このサイトで採用したrobots.txtの出力コードを張ります。 **lf[当サイトのrobots.txt](https://kotoriforblog.com/robots.txt)**

// app/routes/robots[.]txt.ts
import { LoaderFunction } from "@remix-run/node";

export const loader: LoaderFunction = async () => {
  const robotText = `
# Allow general web crawlers
User-agent: *
Allow: /
Disallow: /login/
Disallow: /logout/
Disallow: /edit/
Disallow: /image/
Disallow: /api/

# Block AI/LLM crawlers
User-agent: anthropic-ai
User-agent: Applebot-Extended
User-agent: Bytespider
User-agent: CCBot
User-agent: ChatGPT-User
User-agent: ClaudeBot
User-agent: cohere-ai
User-agent: Diffbot
User-agent: FacebookBot
User-agent: GPTBot
User-agent: ImagesiftBot
User-agent: Meta-ExternalAgent
User-agent: Meta-ExternalFetcher
User-agent: Omgilibot
User-agent: PerplexityBot
User-agent: Timpibot
Disallow: /
`.trim();

  return new Response(robotText, {
    headers: {
      "Content-Type": "text/plain",
      "Cache-Control": "public, max-age=3600",
    },
  });
};

上記のようなコードでLLMクローラの回避、loginページ等のクロールの禁止を行うことができます。 <上記コードの解説> routeフォルダに/robots[.]txt.tsを作りました。これは上記の説明の通り、robots.txt用のルーティングになります。 キャッシュに残る時間を指定することで、検索エンジンボットが頻繁にアクセスすることを防ぎます。 上記のような手順でrobots.txtは実装できました。

エラー: 不明なセクションタイプ

このセクションは正しく表示できません。

セクションタイプ: chat

作者紹介

ことりがさとり

ことりがさとり

IT技術(AI・nextjs・remix)が好き。その他に、読書・ゲーム・e-sports・グルメを中心に記事を書く予定。