Cloudflare guide

How to add a blog at /blog with a Cloudflare Worker

If your domain is on Cloudflare, you can add a blog at yourbrand.com/blog without touching your website, whatever it is built with. A small Cloudflare Worker sends /blog to your free Outrank blog and everything else to your site, as before.

Create my free blog

Free with Outrank Blogs. No credit card needed. See what is included

Subfolder

Your blog at yourbrand.com/blog

This works in front of any website, including site builders that do not let you add rewrites, as long as your domain’s traffic goes through Cloudflare.

Step by step

Set up your blog on Cloudflare

  1. 1Create your free blog: enter your website and pick a template. It is live in about a minute.Start here
  2. 2In Outrank, open Blog → Settings → Domain and choose “Subfolder”. Enter your website address, such as yourbrand.com, keep the path /blog and click Continue. Outrank shows the code for your setup with your blog’s address already filled in.
  3. 3Check that your domain’s DNS is on Cloudflare and that the record for your website has the orange cloud (proxied) turned on.
  4. 4In Cloudflare, open Workers & Pages, create a Worker and replace its code with the snippet below.
  5. 5Add a route yourbrand.com/blog* to the Worker (Settings → Domains & Routes).
  6. 6Click Check now in Outrank.
  7. 7Add a “Blog” link to your site’s header or footer that points to /blog.
worker.js
const ORIGIN_HOST = 'o--your-blog-id.outrankblog.com';
const PREFIX = '/blog';

const rewriteLocation = (location, url) => {
  try {
    const target = new URL(location, `https://${ORIGIN_HOST}`);
    if (target.hostname !== ORIGIN_HOST) return location;
    return `${url.origin}${PREFIX}${target.pathname === '/' ? '' : target.pathname}${target.search}`;
  } catch {
    return location;
  }
};

export default {
  async fetch(request) {
    const url = new URL(request.url);
    if (url.pathname !== PREFIX && !url.pathname.startsWith(`${PREFIX}/`)) {
      return fetch(request);
    }
    if (url.pathname === `${PREFIX}/`) {
      return Response.redirect(`${url.origin}${PREFIX}${url.search}`, 301);
    }
    const upstream = new URL(request.url);
    upstream.hostname = ORIGIN_HOST;
    upstream.pathname = url.pathname.slice(PREFIX.length) || '/';
    const headers = new Headers(request.headers);
    headers.set('X-Forwarded-Host', url.hostname);
    headers.set('X-Forwarded-Proto', 'https');
    headers.set('X-Outrank-Base-Path', PREFIX);
    const response = await fetch(upstream.toString(), {
      method: request.method,
      headers,
      body: request.method === 'GET' || request.method === 'HEAD' ? undefined : request.body,
      redirect: 'manual',
    });
    const proxied = new Response(response.body, response);
    const location = proxied.headers.get('Location');
    if (location) proxied.headers.set('Location', rewriteLocation(location, url));
    return proxied;
  },
};

Example only. In your dashboard, Outrank shows this code with your blog’s own address filled in.

Tips for Cloudflare

  • Cloudflare’s free Workers plan covers 100,000 requests a day, which is plenty for most blogs.
  • If your website uses www, add the route for www.yourbrand.com/blog* too.

Want the posts written for you?

Outrank can research keywords and publish 30 SEO articles a month to your blog, on autopilot.

See plans

FAQ

Adding a blog to Cloudflare: questions

Short answers to what people ask most.

Why put the blog in a subfolder instead of a subdomain?
Search engines treat yourbrand.com/blog as part of your main website, so every post builds the authority of the domain your product pages are on. A subdomain like blog.yourbrand.com works too and is quicker to set up, but it is treated more like a separate site.
Does this work with Webflow, Framer or Shopify?
It works with any website as long as your domain’s traffic goes through Cloudflare’s proxy. Some site builders ask you to keep their DNS records unproxied; in that case, use a subdomain like blog.yourbrand.com instead.
Does connecting my own domain cost anything?
No. Connecting a subdomain, a subfolder or a separate domain is free on every Outrank Blogs plan, and HTTPS is included.

Start today

Your blog can be live before your coffee gets cold.