Skip to content

Instantly share code, notes, and snippets.

@NathanFlurry
Last active September 29, 2025 10:04
Show Gist options
  • Select an option

  • Save NathanFlurry/660c4748c811b9aa04febcbcd97bfec7 to your computer and use it in GitHub Desktop.

Select an option

Save NathanFlurry/660c4748c811b9aa04febcbcd97bfec7 to your computer and use it in GitHub Desktop.
Serve Markdown to agents instead of HTML

Rewrite Markdown URLs For Agents

Prerequisites

  • Serve Markdown files for .md extensions (see #generating-markdown)

Steps

  1. Log in to the Cloudflare Dashboard
    Go to https://dash.cloudflare.com and select your site.

  2. Navigate to Snippets

    • In the sidebar, click Rules > Snippets
    • Click Create Snippet.
  3. Add Your Snippet Code

    • Name your snippet (e.g., docs_url_rewriter).
    • Paste in your code (see below)
  4. Deploy the Snippet

  5. Test Your Snippet

    • Visit your site at https://example.com/docs/.... Confirm this works as expected
    • Then call curl "https://example.com/docs/..." to verify that you get served the markdown version
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
// Check if the path starts with /docs/ and Accept header is NOT text/html
const acceptHeader = request.headers.get("accept") || "";
if (
url.pathname.startsWith("/docs/") &&
!acceptHeader.includes("text/html")
) {
// Rewrite the URL to add ".md" at the end if it doesn't already have it
if (!url.pathname.endsWith(".md")) {
url.pathname = `${url.pathname}.md`;
}
// Create a new request with the rewritten URL
request = new Request(url.toString(), request);
}
return await fetch(request);
},
};
@NathanFlurry
Copy link
Author

CleanShot 2025-09-28 at 15 17 46@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment