TLDR: Is there any way I can capture the query parameter to use it in my redirect? Our website used to use Next.js built-in Image component, which alters the URLs of images in such a way that prevents them to be indexed by Google. We opted out of Image optimization to use the original URLs, but the old URLs need to be redirected to original ones for Google to remove from the blocklist. Old URL: /_next/image?url=https%3A%2F%2Fcdn.pellaglobal.net%2Ficons%2Ficon-campaigns.svg&w=640&q=80 New URL: https://cdn.pellaglobal.net/icons/icon-campaigns.svg
What steps have you taken to resolve the issue?
I tried some redirect or rule as attached below, but it does not seem to work either.
I want to catch the value of “url” query parameter; that is, "after ?url= part, and an example I want to end up with can be the one you said:
I also have one obstacle that the URLs show up like https://pellaglobal.net/_next/image?url=https%3A%2F%2Fcdn.pellaglobal.net%2Ficons%2Ficon-on-page-seo-optimization.svg&w=640&q=75, which means I might need to change the URLs based on the encoding (%3A to : or %2F to /).
I need help on the matter of what matching or regex I should use and on which part: Page rule, Redirect rule, or something else?
If your query strings are dynamic, you could potentially solve this with two redirect rules:
one that targets https://pellaglobal.net/_next/image?*url=*&* — this one will match on url values in the middle of the query string; the target URL can then be ${2};
another one that targets https://pellaglobal.net/_next/image?*url=* — this one will match on url values at the end of the query string; same target URL ${2} here.
Now, if you need to fix the URL encoding, you can use url_decode() function of the Ruleset Engine. However, this means that you will need to move from simple Wildcard pattern UI of Redirect Rules to the more advanced Custom filter expression. In which case, your rules will look like this:
Alternatively, if you’re comfortable with JavaScript, you can describe your custom logic via Snippets and parse query strings as you like to serve redirects directly from there. Here is an example on how to interact with query strings in Snippets, and here is how to serve a redirect.