Home
Support Forums

Cannot use next/image component with rewrite from my netlifySiteA to my netlifySiteB

Hi,
I have siteA in Gatsby and siteB in NextJS, both deployed to Netlify.
In siteA I have those rewrites:

[[redirects]]
from = "/pl/baza-wiedzy"
to = "https://elastic-jepsen-1ed2cc.netlify.app/pl/baza-wiedzy"
status = 200

[[redirects]]
from = "/pl/baza-wiedzy/*"
to = "https://elastic-jepsen-1ed2cc.netlify.app/pl/baza-wiedzy/:splat"
status = 200

[[redirects]]
from = "/_next/*"
to = "https://elastic-jepsen-1ed2cc.netlify.app/_next/:splat"
status = 200

[[redirects]]
from = "/nextimg/*"
to = "https://elastic-jepsen-1ed2cc.netlify.app/nextimg/:splat"
status = 200

[[redirects]]
from = "/_next/image"
to = "https://elastic-jepsen-1ed2cc.netlify.app/_next/image"
status = 200

In siteB I have this rewrite:

[[redirects]]
from = "/pl/baza-wiedzy"
to = "/pl/knowledge-base"
status = 200

When I go to https://elastic-jepsen-1ed2cc.netlify.app/pl/baza-wiedzy (siteB) the hero image loads properly, but the problem appears when I go to https://boring-pike-48ee77.netlify.app/pl/baza-wiedzy (siteA), hero image doesn’t load.
When I look on the siteB’s network at chrome dev tools I can see the request for optimized image that gets 301 with optimized image location (please check network by yourself. As a new forum user I can’t put more than one screenshot here :sweat_smile:)

When I look on the siteA’s network the request gets 404 (please check network by yourself.)

Is it common problem with rewrites with NextJS or did I miss something important in my rewrites? :thinking:

Netlify processes redirect rules from top to bottom. So the rule that matches first is executed and rest are ignored. In this case, your last redirect rule is probably never being used as all of it is getting matched by the rule I quoted here.

I’d try to move the /_next/image rule above this one to see if it makes a difference. Technically speaking, even if the first rule is matching the image folder, it should have worked due to the fact that it’s still passing the correct URL. But it’s possible that there are other things in between that are causing it to fail.

1 Like