You cannot match query strings in nginx rewrite
directive. You have to do something like this:
location ~* /(?<p1>[a-z]+)-(?<p2>[a-z]+)-(?<p3>[a-z]+) {
if ($args ~ id=(?<q1>[a-z]+)-(?<q2>[a-z]+)) {
rewrite ^ http://example.com/$p1$p2$p3?id=$q1$q2 permanent;
}
}
Here we use regular expression captures to capture parts of the URL between dashes to different nginx variables (p1
, p2
and p3
) ?<p1>
after (
means that the match should be stored to p1
variable.
Then, if the location block matches, then we try to match the query string ($args
in nginx), if it contains two words separated by dashes. If a match is found, the parts are stored to q1
and q2
.
Finally, we perform the actual rewrite using the captured parts.