Cloudflare Workers: Route Your Blog to a Subdirectory (2025 Guide)
Cloudflare Workers can front your blog and present it at /blog without rebuilding your CMS. Here’s how to do it and when BlogPath.io is the faster path.
Table of Contents
- When to use Workers for /blog
- High-level architecture
- Worker routing recipe
- Cache strategy
- SEO and redirects
- Testing and rollback
- Observability and logs
- Advanced FAQ
- Why choose BlogPath.io
- Related posts
When to use Workers for /blog
- You control DNS on Cloudflare and want low-latency edge logic.
- You need lightweight path rewrites and some header normalization.
- You’re comfortable maintaining a small JS worker.
High-level architecture
- User → Cloudflare DNS → Worker → fetch origin (e.g.,
blog.origin.example.com) → return at/blog/*. - Keep origin SSL; only expose through Cloudflare.
Worker routing recipe
- Match
/blog/*and strip/blogbefore fetching origin. - Preserve query strings and headers; forward real IP via
CF-Connecting-IP. - Bypass cache for admin/login paths.
- Example snippet (conceptual):
if (url.pathname.startsWith('/blog/')) {
url.hostname = 'blog.origin.example.com'
url.pathname = url.pathname.replace('/blog', '')
// add headers, cache rules
}
Cache strategy
- Long TTL for CSS/JS/images; short TTL or
stale-while-revalidatefor HTML. - Bypass on auth cookies; respect origin cache-control when needed.
SEO and redirects
- Set canonicals to
/blogURLs. - 301 legacy subdomain URLs to
/blog/*. - Ensure sitemap/feeds are reachable at
/blog/sitemap.xmlor update references.
Testing and rollback
- Use a preview host before swapping DNS.
- Keep DNS TTL at 300s for fast rollback.
- Monitor 404/500s in Cloudflare logs; validate Core Web Vitals after launch.
Observability and logs
- Enable Cloudflare Logs or GraphQL analytics for 4xx/5xx, cache status, and latency.
- Track cache hit ratio by path; tune rules for image-heavy posts.
- Alert on spikes in 520/522/525 errors (often origin reachability).
- Inspect user agents and geos to refine bot challenges.
Advanced FAQ
Do I need multiple Workers?
Usually one Worker is enough; keep logic small. Use KV/Durable Objects only if you add stateful features (rare for blogs).
How do I secure the origin?
Allow only Cloudflare IPs; block direct access. If your host supports it, use authenticated origin pulls.
Can I rewrite cookies?
Avoid changing auth cookies. Bypass cache on auth cookies; cache HTML for anonymous sessions with short TTLs.
What about images and video?
Let Cloudflare cache media with long TTLs; consider Polish/Images/Stream if you want more optimization, but test for parity.
Why choose BlogPath.io
- Global CDN caching and origin shielding out of the box.
- No-code setup—DNS change instead of writing Workers.
- Reliability/SLA and observability without maintaining scripts.
- Faster time-to-value vs hand-rolled Workers; reversible in minutes.