Home > Blockchain >  Replacing one of the parameters in the URL having a query string and retaining the other parameters
Replacing one of the parameters in the URL having a query string and retaining the other parameters

Time:10-11

I'm trying to replace a URL parameter value with the Apache rewrite.

RewriteCond ℅{QUERY_STRING} market=value [NC]
RewriteRule ^/page.html /page.html?market=new-value [R=301,L,NC]

The URL pattern is https://example.com/page.html?market=value&store=value

I need to replace only the market parameter value and retain the store parameter as is.

CodePudding user response:

RewriteRule ^/page.html /page.html?market=new-value [R=301,L,NC]

Note that when used in .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash, so the regex ^/page.html used above will never match in this context.

Try the following instead:

RewriteCond %{QUERY_STRING} ^(.*&)?(market=)value(&.*)?$
RewriteRule ^/?page\.html$ %{REQUEST_URI}?%1%2new-value%3 [NE,R=301,L]

This looks for the market=value URL parameter name/value pair anywhere in the query string. We capture the parts of the query string that surround the market URL parameter value (including the parameter name itself). These are saved in the following backreferences:

  • %1 / pattern (.*&)? - the part of the query string before the market URL parameter (if any). This includes the & delimiter before the market URL parameter name.
  • %2 / pattern (market=) - captures the literal text market=. This must match to be successful. This simply saves repetition later.
  • %3 / pattern (&.*)? - the part of the query string after the market URL parameter (if any). This includes the & delimiter after the market URL parameter value.

The REQUEST_URI server variable in the substitution string contains the full root-relative URL-path. We follow this with the query string that we rebuild using the backreferences as explained above, injecting new-value as the value of the market URL param (replacing the value this URL parameter had originally).

The NE (noescape) flag is required to prevent the & (URL param delimiter) being URL-encoded in the response.

Note that I removed the NC (nocase) flag from both the RewriteCond and RewriteRule directives. URL parameter names are not normally case-insensitive and this rule (as written) will preserve the case used in the request.

Test first with a 302 (temporary) redirect to avoid potential caching issues, even if you change this to a permanent redirect later. And you will need to clear your browser cache before testing, since the earlier 301 (permanent) redirect will have been cached by the browser.

  • Related