How to Serve a Subdomain as a Subdirectory Using Cloudflare
Using a subdirectory on an established domain is almost always better for SEO than using a subdomain. But what happens when you have two applications you want available in the same domain? This tutorial will show you how easy it is to serve two different websites on one domain.
Update: Methods in this post are outdated. I have an updated API that manages this all for you.
You're probably an SEO-conscious person who wants the best of both worlds. That is to use two different websites/CMSs/applications on the same domain without using a subdomain.
But, wait! Google says using a subdomain doesn't impact SEO!?
That is true, but anything that Google says has to be analyzed.
Watch this tutorial in my video or read ahead below.
Subdomain vs. Subdirectory
I believe the correct interpretation of Google's statement is as follows. When considering a brand new domain, creating a subdomain will be given the same fair shot at achieving rankings as the root domain. So it is true that subdomains and root domains have equal treatment.
But here's where this unravels.
If we are considering creating a brand new subdomain on a domain that has built up credibility and traffic over time, they will not be treated equally. The root domain has established its dominance, but the subdomain has not. I'm not sure if the subdomain is treated as a fresh domain or if you're given some sort of boost because it's piggybacking off the root domain. I do know that it won't perform as well as the primary domain. Here are 14 case studies demonstrating subdomains getting moved to subdirectories and their traffic significantly increasing.
Using Cloudflare to Serve a Subdomain as a Subdirectory
Okay, we've established you want to use two different websites but not use a subdomain. Lucky for you, it's pretty easy to serve your subdomain (e.g., blog.example.com) as a subdirectory (e.g., example.com/blog). This trick I'm going to share with you is very cool and effective.
First, you need your DNS routed through Cloudflare and for the traffic to be proxied (i.e. the clouds must be orange).
Next, go to Workers and create one.
Next, paste the following code in:
Note: I've updated the code in this snippet and the images are showing the older version. They'll both work, but this version allows for more customization and it rejects post requests which this proxy can't handle.
Here's what it looks like in the dashboard:
Be sure to change out the URLs to yours. The first is your root domain, and the second is the subdomain/other website. In summary, the code looks at the URL being requested and swaps it out with another URL (while still making it appear to come from the URL requested).
Next, we are going to tell Cloudflare on which URLs the code should be executed.
Go back to Workers and Add Route. Here is where we tell Cloudflare our subdirectory path(s) to run this code. Add the path *example.com/blog*. The asterisks are wild cards, so the URL will match additional subdirectories. Add the Worker we created and save it.
After about 1 minute, you should be able to visit your website subdirectory and see your subdomain. Pretty cool!
Downsides of Proxying Your Subdomain Like This
You can only retrieve data from the server (GET requests); you cannot send data (POST requests). That means you can't log in or submit forms.
You can submit forms if you use an embed of a form (e.g., Mailchimp signup) as those requests are made against their servers.
Update: I needed to submit forms on this setup and I did so by modifying the form action to be an absolute URL.
I hope this has worked for you and that you achieve a huge SEO boost! Feel free to reach out and hire me if you need assistance.