Serving a subdomain from subdirectory using Cloudflare workers

Route a subdirectory to a subfolder with cloudflare

How I Used Cloudflare Workers to Fix a Critical SEO Problem (And You Can Too)

Ever had that moment when you realize your clever technical solution is actually killing your SEO? Yeah, me too. Here’s how I fixed it using Cloudflare Workers, and saved my e-commerce site’s search rankings in the process.

The SEO Problem: Subdomains vs. Subdirectories

Look, I’ll cut to the chase – Google treats subdomains as separate websites. This means if you’re building valuable content on a subdomain, you’re essentially splitting your SEO juice between different sites. Not ideal when you’re trying to rank your main domain.

I hit this exact problem with my watch strap e-commerce site. I’d built this fancy “Strap Finder” tool (think faceted search on steroids) using C# and Azure. It was beautiful, it was fast, but it was living on a subdomain (strapfinder.watchstraps.com.au). SEO nightmare territory.

Strapfinder

The Technical Solution: Enter Cloudflare Workers

Here’s where it gets good. Using Cloudflare Workers, I managed to serve my subdomain content from a subdirectory instead. This means all that SEO value stays with my main domain. Win!

Here’s the code that made it happen:

javascriptCopyconst mySubdomain = {
    hostname: "strapfinder.watchstraps.com.au",
    targetSubdirectory: "/strapfinder",
    assetsPathnames: ["/strapfinder-lib/", "/strapfinder-css/", "/strapfinder-js/"]
}

async function handleRequest(request) {
    const parsedUrl = new URL(request.url)
    
    if (parsedUrl.pathname.startsWith(mySubdomain.targetSubdirectory)) {
        return fetch(`https://${mySubdomain.hostname}/${parsedUrl.pathname}`)
    }
    return fetch(request)
}

addEventListener("fetch", event => {
    event.respondWith(handleRequest(event.request))
})

Now, when someone visits watchstraps.com.au/strapfinder, they’re actually seeing content from strapfinder.watchstraps.com.au, but all the SEO value stays with my main domain. Slick, right?

Why This Matters For Your Business

This solution isn’t just for watch strap websites. You could use this same approach if you’re:

  • Running a Shopify store with a WordPress blog
  • Managing multiple web apps under one brand
  • Building tools or resources that complement your main site

The Results

Since implementing this solution, all the SEO value from my Strap Finder tool flows directly to my main domain. Plus, the user experience remains exactly the same – they never even know they’re technically accessing a subdomain.

Want to Chat About Technical SEO?

I love geeking out about this stuff. If you’re facing similar challenges or just want to discuss technical SEO solutions, hit me up on LinkedIn. I’m always keen to connect with other tech-minded folks in the digital space.


Brad Farleigh is the CTO at Bang Digital in Perth, Australia, where he helps businesses solve complex technical marketing challenges. When he’s not coding or optimizing websites, he’s running his e-commerce business and probably thinking about ways to make it faster.

Leave a Reply

Your email address will not be published. Required fields are marked *