Can I capture the query parameter to use it in my redirect?

What is the name of the domain?

What is the issue you’re encountering

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.

Screenshot of the error

Using Wildcard is lately supported, and yes you can:

Is this the part which you’d like to catch? Or rather after ?url=?

Does the part:
https://cdn.pellaglobal.net/icons/icon-campaigns.svg

Change or? :thinking:

Helpful case as example:

1 Like

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:

  1. 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};
  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:

  • Filter expression: (http.request.full_uri wildcard "https://pellaglobal.net/_next/image?*url=*&*")
  • Then: URL redirect > Dynamic
  • Expression: url_decode(wildcard_replace(http.request.full_uri, "https://pellaglobal.net/_next/image?*url=*&*", "${2}"))

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.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.