0

I am using this rewrite rule to redirect

example.com/a-b-c?id=learn-more ----> http://example.com/abc?id=learnmore

rewrite ^/a-b-c?id=learn-More http://example.com/abc?id=learnMore permanent

But its not working !! It is redirecting to

example.com/abc?id=learn-more (learn-more is not converted to learnmore).

How to achieve this?

3 Answers 3

1

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.

1

If you just have one (or maybe a few) of these to match exactly, then you can check the argument explicitly.

if ($arg_id = "learn-More"} {
    return 301 $scheme://$http_host$uri?id=learnMore
}
0

You will need to escape the ? in the regular expression; as it stands, it indicates that the c is optional.

Try: rewrite ^/a-b-c\?id=learn-More http://example.com/abc?id=learnMore permanent

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .