const SITEURL = 'site.com'; const WP_HOST = 'wp.site.com'; const langs = new Set([ '/nl', '/be', '/de', ]); const pathsToRedirect = new Set([ '/account', '/search', '/custom-domain', ]); async function handleRequest(request, env) { let url = new URL(request.url); console.log('Hostname:', url.hostname); console.log('Pathname:', url.pathname); // Redirect www to non-www if (url.hostname !== SITEURL) { console.log(url.hostname); if (url.pathname.length <= 1) { const cd = await handleCustomDomain(request, env); if (cd !== 0) { const reqHost = `https://${SITEURL}`; const targetUrl = `${reqHost}/custom-domain/custom-home`; const response = await fetch(targetUrl, { method: request.method, headers: request.headers, body: request.body, }); const newResp = new Response(response.body, { status: response.status, statusText: response.statusText, headers: response.headers, }); newResp.headers.set('Access-Control-Allow-Origin', '*'); return newResp; } else { url.hostname = SITEURL; return Response.redirect(url.toString(), 301); } } } if (!url.pathname.includes('wp') && (url.pathname.includes('.js') || url.pathname.includes('.css'))) { const reqURL = `https://${SITEURL}`; return await fetch(reqURL + url.pathname, request); } if (url.pathname.includes('wp')) { return handleBlog(request); } if ( url.pathname === '/' || (langs.has(url.pathname) && url.pathname.endsWith('')) || (langs.has(url.pathname.substring(0, 3)) && url.pathname.length <= 4) ) { return await fetch(request); } // Additional conditions for blog handling if (url.searchParams.get('wpml-app') === 'ate-widget' || url.searchParams.has('s')) { return handleBlog(request); } const pathParts = url.pathname.split('/'); const lang = '/' + pathParts[1]; let path = '/' + pathParts[2]; if (langs.has(lang) && pathsToRedirect.has(path)) { return await fetch(request); } // edge case where language code is not there in URL path = '/' + pathParts[1]; if (pathsToRedirect.has(path)) { return await fetch(request); } return handleBlog(request); }