Reputation: 119
We were running on apache previously and used the below code to redirect/replace one of our query parameters.
RewriteCond %{QUERY_STRING} ^limit=(.*)$ [NC]
RewriteRule ^ %{REQUEST_URI}?product_list_limit=%1 [L,R=301]
This would successfully redirect the below URL
www.example.com/clothing.html?limit=all
TO
www.example.com/clothing.html?product_list_limit=all
I have used a htaccess redirect convertor that has produced the below. This doesn't work
location ~ / {
if ($query_string ~* "^limit=(.*)$"){
rewrite ^(.*)$ /$request_uri?product_list_limit=%1 redirect;
}
}
Any help appreciated
Thanks
Upvotes: 2
Views: 2821
Reputation: 49692
You should not use $request_uri
to construct the rewritten URI, as it also contains ?limit=all
- so your rewritten URI would look like: /clothing.html?limit=all?product_list_limit=all
The rewrite
statement resets numeric captures, so you will need to use a named capture in the if
statement.
The URI without query string is available as $uri
or as a capture from the rewrite
statement's regular expression.
Either of these forms should work for you:
if ($query_string ~* "^limit=(?<limit>.*)$") {
rewrite ^(.*)$ $1?product_list_limit=$limit? redirect;
}
Note the trailing ?
to prevent the original query string from being appended. See this document for more.
Or:
if ($query_string ~* "^limit=(.*)$") {
return 302 $uri?product_list_limit=$1;
}
The value of the limit
argument is also available as $arg_limit
, so you could also use this:
if ($arg_limit) {
return 302 $uri?product_list_limit=$arg_limit;
}
See this document for details.
Upvotes: 1