Re-direction not happening on detecting 404

Hey all - please see the section of worker code below. The specific portion NOT happening is the re-direction to /404.html.

In other words, I suspect this bit is fishy

return new Response('', { status: 404, headers: { 'Location': 'https://go.tallyfy.com/404.html' } });

Any reason why? Appreciate any pointers!

if ( u.hostname.startsWith("api.tallyfy.com") || u.hostname.startsWith("go.tallyfy.com") ) {
		// Check if it's a blocked country
		var blockedCountries = ["CU","HK","IR","LB","SY","KP","LY","SD","SO","CN","VE","IQ","RU","YE","ZW","BY"];
		var isFromBlockedCountry = false;
		for (var i = 0; i < blockedCountries.length; i++) {
		  if (blockedCountries[i] == (request.headers.get("CF-IPCountry"))) {
			isFromBlockedCountry = true;
			break;
		  }
		}
		
		if (isFromBlockedCountry == true ) {
			return fetch("https://tallyfy.com/sorry", request);
		}		
		else {
			// Check if it's an API request or not for future logging
			var isProductionAPIRequest = false;
			if ( u.hostname.startsWith("api.tallyfy.com") || u.pathname.startsWith("/api") ) {
				isProductionAPIRequest = true;
			}
			if (!isProductionAPIRequest) {
				const prodres = await fetch(request);
				if (prodres.status === 404) {
					return new Response('', { status: 404, headers: { 'Location': 'https://go.tallyfy.com/404.html' } });        	
				}
				else {
					return prodres;
				}
			}
			else {
				if ( u.hostname.startsWith("api.tallyfy.com") ) {
					return fetch("https://go.tallyfy.com/api" + u.pathname, request);
				}
				return fetch(request);
			}			
		}				
	}

You need to use a 301 or 302 response for a redirect – browsers won’t redirect on a 404 response.

You’re right. I had a general “moment” here. Thanks @michael.hart

1 Like