export default { async fetch(request) { const url = new URL(request.url); const pathname = url.pathname; const hostname = url.hostname; const isBlogPath = pathname.startsWith("/blog"); const isAdminPath = pathname.includes("/wp-admin") || pathname.includes("/wp-login.php") || pathname.includes("/wp-json") || pathname.includes("/xmlrpc.php") || pathname.includes("/admin-ajax.php"); const cookie = request.headers.get("cookie") || ""; const hasAuthCookie = cookie.includes("wordpress_logged_in") || cookie.includes("wordpress_sec") || cookie.includes("wp-settings-"); // ===================================================== // 1. FORCE API TO SUBDOMAIN // ===================================================== if (pathname.startsWith("/blog/wp-json")) { return Response.redirect( "https://blog.rtspvtltd.com/wp-json" + pathname.replace("/blog/wp-json", ""), 302 ); } // ===================================================== // 2. FORCE ADMIN TO SUBDOMAIN // ===================================================== if (pathname.startsWith("/blog/wp-admin")) { return Response.redirect( "https://blog.rtspvtltd.com/wp-admin", 302 ); } // ===================================================== // Redirect subdomain → main domain /blog // (except admin & API) // ===================================================== if (hostname === "blog.rtspvtltd.com" && !isAdminPath) { return Response.redirect( "https://rtspvtltd.com/blog" + pathname, 301 ); } // ===================================================== // Proxy /blog → subdomain // ===================================================== if (isBlogPath) { const newUrl = new URL(request.url); newUrl.hostname = "blog.rtspvtltd.com"; newUrl.pathname = newUrl.pathname.replace("/blog", "") || "/"; // No cache for admin or logged-in users if (isAdminPath || hasAuthCookie) { return fetch(newUrl, { cf: { cacheTtl: 0, cacheEverything: false } }); } // Static files (1 day cache) const isStatic = /\.(jpg|jpeg|png|gif|webp|svg|css|js|woff|woff2|ttf|ico)$/i.test(newUrl.pathname); if (isStatic) { return fetch(newUrl, { cf: { cacheTtl: 86400, cacheEverything: true } }); } // HTML (short cache) return fetch(newUrl, { cf: { cacheTtl: 300, cacheEverything: true } }); } // ===================================================== // Default // ===================================================== return fetch(request); } };