Get the most out of your Centmin Mod LEMP stack
Become a Member

Nginx How to redirect a URL in Nginx?

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by rdan, Jun 6, 2019.

  1. rdan

    rdan Well-Known Member

    4,545
    1,088
    113
    May 25, 2014
    Ratings:
    +1,588
    Local Time:
    3:45 PM
    Mainline
    10.2
    For some reason this isn't working for me.
    Code:
    rewrite /register/facebook?reg=1 /register/connected-accounts/facebook/?setup=1 permanent;
    But this one works fine:
    Code:
    rewrite /account/external-accounts /account/connected-accounts/ permanent;

     
  2. rdan

    rdan Well-Known Member

    4,545
    1,088
    113
    May 25, 2014
    Ratings:
    +1,588
    Local Time:
    3:45 PM
    Mainline
    10.2
    This one works for me.
    Code:
    rewrite ^(/register/facebook*) /register/connected-accounts/facebook/?setup=1        permanent;
    rewrite ^(/register/twitter*) /register/connected-accounts/twitter/?setup=1          permanent;
    Don't know how to convert this into location block, as it's recommended than rewrite.
     
  3. eva2000

    eva2000 Administrator Staff Member

    40,188
    8,888
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +13,697
    Local Time:
    5:45 PM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    it's because your 1st has a argument for reg=1 that you need to properly deal with ;)

    I'll give you a hint, first narrow and match the location /register/facebook and then within that location context match the nginx argument reg=1 to rewrite to the desired destination url
     
  4. rdan

    rdan Well-Known Member

    4,545
    1,088
    113
    May 25, 2014
    Ratings:
    +1,588
    Local Time:
    3:45 PM
    Mainline
    10.2
    Works for me:
    Code (Text):
    location = /register/facebook  { return 301 https://site.com/register/connected-accounts/facebook/?setup=1; }
    location = /register/twitter  { return 301 https://site.com/register/connected-accounts/twitter/?setup=1; }
    location = /register/google  { return 301 https://site.com/register/connected-accounts/google/?setup=1; }
     
  5. eva2000

    eva2000 Administrator Staff Member

    40,188
    8,888
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +13,697
    Local Time:
    5:45 PM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    yeah that would work if you don't care about a specific query string argument match i.e. ?reg=1
     
..