0

I want to redirect some URL to another folder on the same website. Each URL have query string so simple 301 return is not working.

Example of redirect I want.

example.com/th/?n=abc 301 to example.com/thh/?n=abc
example.com/th/?n=xyz 301 to example.com/thh/?n=xyz

After searching solution, I found a solution that works for only first URL.

 location ~ /th/ {
    if ($args ~* "^n=abc") {
        set $mid $1;
        rewrite ^.*$ https://example.org/thh/$mid permanent;
    }
   }

But if I create the same rule for xyz query string the redirect rule do work.

location ~ /th/ {
        if ($args ~* "^n=xyz") {
            set $mid $1;
            rewrite ^.*$ https://example.org/thh/$mid permanent;
        }
       }

The second rule doesn't work only first one works. I thought might be mid var is getting overwritten by the first rule. I changed var to jar still it doesn't work.

Could someone please help find the solution for it. I don't want to redirect whole th folder to thh.

  • The first location block will process any URI containing /th/. You should probably place all of your statements within the same location block. What is $mid supposed to do? In your configuration, $mid is empty, because $1 is empty, because there are no captures in your regular expressions. – Richard Smith Dec 25 '18 at 9:41
  • I tried to putting whole query string under location block but it simply won't redirect. – user3259496 Dec 25 '18 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.