Using Cloudflare Workers to proxy a WordPress blog to domain.tld/blog

TL;DR: If you have a WordPress blog deployed on a subdomain of your main website, and you want to show it under /blog for SEO purposes, use Cloudflare Workers.

Assumptions:

  • You have a sales or ecommerce website deployed on domain.tld.
  • You have a WordPress blog deployed on blog.domain.tld.
  • You want to serve the WordPress blog under the domain.tld/blog subpath for SEO purposes.
  • You are using Cloudflare as your DNS provider.

Before we begin, if you don’t have a blog yet, or don’t want to go through this whole process yourself, sign up to PressProxy and let us handle it for you.

Cloudflare Workers

This guide assumes you are already using Cloudflare for DNS hosting of your website. If you are not using Cloudflare yet, follow this nice guide first.

In your Cloudflare account, deploy a new Worker that will proxy (not redirect!) requests from blog.domain.tld to domain.tld/blog. To your browser, it would seem as if WordPress is deployed on https://domain.tld/blog.

Edit the code of your new worker to be like the snippet below. Make sure to update the url.host in line 6 to your domain.

export default {
  async fetch(request) {
      let url = new URL(request.url);

      if (url.pathname.startsWith("/blog")) {
          url.host = "blog.domain.tld";
          let newRequest = new Request(url, request);
          return await fetch(newRequest, {
              cf: {
                  cacheTtlByStatus: { "200-299": 5, 404: 5, "500-599": 0, "300-310": 0 },
                  cacheEverything: true,
              },
          });
      }
      return fetch(request);
  },
};

With the worker successfully saved and deployed, add a “Route” trigger for it in Settings.

Cloudflare Website Configuration

The proxy worker is now ready for use. But first, make sure that your website has the necessary Cloudflare features enabled.

Verify that your root domain is set to proxied and not “DNS only”.

Verify that SSL/TLS Encryption Mode is set to “Full”.

Note that your WordPress needs to have SSL enabled as well, and be available on https://blog.domain.tld. If you don’t want to deal with this, sign up with PressProxy, and we’ll do it for you.

WordPress Configuration

The final piece of the puzzle is to configure your WordPress to use domain.tld/blog in generated links. Login to your WordPress blog and update the “Site Address” to point to https://domain.tld/blog.

It is important to leave “WordPress Address” as is, on a subdomain! You will use the subdomain to access the Admin Panel to manage content. It’s best to add a bookmark in your browser for https://blog.domain.tld/wp-admin.

Because of proxying, Gutenberg Full Site Editor and similar Page Builders will not work. While you can still add content to the blog, you won’t be able to design the blog through the UI. It is best to install the Classic Editor for easy writing and editing posts.

Note that if you’re using Yoast or similar SEO plugins, you will have to manually configure them to work with the new domain.tld/blog URL.

Finally, in order to avoid duplicate content, you will need to install plugins to handle redirects from blog.domain.tld to domain.tld/blog and to modify robots.txt on blog.domain.com to prevent indexing.

Pitfalls & Troubleshooting

Here’s a list of common pitfalls to check if you are having troubles setting up proxying.

  • You have to use the Classic Editor.
  • You have to re-configure any SEO and redirect plugins.
  • Your blog hosting needs to have SSL configured, so the blog can load on HTTPS.
  • You have to bookmark the login page as there is no other way to access it: https://blog.domain.tld/wp-login
  • If you are using a big-brand page builder that uses Cloudflare Enterprise, you will get a “Direct IP access not allowed” error when proxying. You need to reach out to Cloudflare to get “O2O” feature enabled for your account.

PressProxy takes care of all of the above and more, and we offer free transfer for existing blogs!