Cloudflare worker to load content from subdomain/alternate location and replace references to subdomain/alternate location.
addEventListener('fetch', event => { var url = new URL(event.request.url); if (url.pathname.startsWith('/blog') || url.pathname === '/blog') { event.respondWith(handleBlog(event, url)); } else { event.respondWith(fetch(event.request)); } }) async function handleBlog(event, url) { // Load subdomain content / reverse proxy mysite.com/blog to blog.mysite.com subdomain var originUrl = url.toString().replace('https://mysite.com/blog', 'https://blog.mysite.com'); // Load content let response = await fetch(originUrl); // Make sure we only modify text, not images let type = response.headers.get("Content-Type") || ""; if (!type.startsWith("text/")) { return response; } // Read response body let text = await response.text(); // Modify it let modified = text.replace(/blog.mysite.com/g, "mysite.com/blog") // Return modified response return new Response(modified, { status: response.status, statusText: response.statusText, headers: response.headers }); }
Quick Links
Legal Stuff