How can I rewrite to a URL that redirects without it affecting my own URL structure?
Unanswered
American Bobtail posted this in #help-forum
American BobtailOP
I have the following code in my NextJS middleware to handle images fetching
This code is supposed to take pathname
if (LOCATION_IMAGE_REGEX.test(request.nextUrl.pathname)) {
const matches = request.nextUrl.pathname.match(LOCATION_IMAGE_REGEX);
const width = matches.at(1);
const height = matches.at(2);
const code = matches.at(3);
const extension = matches.at(5);
return NextResponse.rewrite(`https://photo.hotellook.com/static/cities/${width}x${height}/${code}.${extension}`);
}This code is supposed to take pathname
/assets/image/location/310/310/ACI.webp and rewrite it to https://photo.hotellook.com/static/cities/310x310/ACI.webp. It worked just fine until https://photo.hotellook.com/static/cities/310x310/ACI.webp started redirecting to https://photo.hotellook.com/static/cities/310x310/30295.webp. So now URLs in my own app is changing from assets/image/location/310/310/ACI.webp to /static/cities/310x310/30295.webp and image obviously cannot be found. How can I fix this? I could catch create another middleware handler that would rewrite /static/cities/310x310/30295.webp to https://photo.hotellook.com/static/cities/310x310/30295.webp but I still want to serve those images from /assets/image/location/310/310/30295.webp