Valak JL
CDCloudflare Developers
•Created by Valak JL on 10/4/2024 in #workers-help
Worker code for workaround to redirect pages with multi-level paths
I've been trying to set up a redirect for a list of URLs with multi-level paths to another multi-level path through the GHL platform, this hasn't been working out so I am now trying a worker workaround in which I input the following code:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url)
// Match specific paths or full URIs
if (
url.pathname === '/collections/hoodies'
url.pathname === '/collections/hoodies?page=1'
url.pathname === '/collections/hoodies?page=2' ||
request.url === 'https://exampledomain.com/collections/hoodies'
) {
// Redirect to the new URL
return Response.redirect('https://exampledomain.com/products-list/collections/hoodies', 301)
}
// If the request doesn't match, pass it through
return fetch(request)
}
I am currently getting this error when visiting the worker page: There is nothing here yet
If you expect something to be here, it may take some time.
Please check back again later.
10 replies