Want to subscribe to topics you're interested in?
Become a Member

Nginx Redirect from /forum/ to /forums/

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by pamamolf, Mar 24, 2020.

  1. pamamolf

    pamamolf Premium Member Premium Member

    3,991
    416
    83
    May 31, 2014
    Ratings:
    +806
    Local Time:
    3:55 AM
    Nginx-1.17.x
    MariaDB 10.3.x
    Hello

    I am trying to redirect from forum to forums:
    Code:
    https://domain.com/forum/topic/blabla
    to
    Code:
    https://domain.com/forums/topic/blabla
    I tested this:
    Code:
    location ~ /forum(.*) {
      rewrite ^(.*)$ /forums$1 redirect; 
    }
    and this:
    Code:
    location ~ /forum/(.*) {
      rewrite ^(.*)$ /forums/$1 redirect; 
    }
    result was:
    Code:
    https://domain.com/forums/forums/forums/forums/forums/forums/forums/forums/forums/forums/forums/forums/forums/forums/forums/forums/forums/forums/forums/forum/topic/
    then i tested:
    Code:
    location ~ ^/forum(.*)$ {   
        return 301 /forums$1;   
    }
    and this:
    Code:
    location ~ ^/forum/(.*)$ {   
        return 301 /forums/$1;   
    }
    result was:
    Code:
    https://domain.com/forumsssssssssssssssssss/topic/
    Any ideas how can i fix that?

    Thank you

     
  2. JoeDer

    JoeDer Member

    82
    19
    8
    Feb 22, 2015
    Ratings:
    +48
    Local Time:
    3:55 AM
    Nginx 1.21.x
    MariaDB 10.3.x
    Code:
    location = /forum {
      return 301 /forums;
    }
    
    or
    Code:
    location ~ /forum/(.*)$ {
       rewrite ^ /forums/$1?$args permanent;
    }
    
     
  3. eva2000

    eva2000 Administrator Staff Member

    49,299
    11,296
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,575
    Local Time:
    10:55 AM
    Nginx 1.21.x
    MariaDB 10.x
    Code (Text):
    location ~ ^/forum/?(.*)$ {
        return 301 /forums/$1;
    }
    

    Code (Text):
    curl -I http://domain.com/forum
    HTTP/1.1 301 Moved Permanently
    Date: Tue, 24 Mar 2020 13:12:46 GMT
    Content-Type: text/html
    Content-Length: 162
    Location: http://domain.com/forums/
    Connection: keep-alive
    Server: nginx centminmod
    X-Powered-By: centminmod
    

    Code (Text):
    curl -I http://domain.com/forum/
    HTTP/1.1 301 Moved Permanently
    Date: Tue, 24 Mar 2020 13:12:50 GMT
    Content-Type: text/html
    Content-Length: 162
    Location: http://domain.com/forums/
    Connection: keep-alive
    Server: nginx centminmod
    X-Powered-By: centminmod
    

    Code (Text):
    curl -I http://domain.com/forum/topic
    HTTP/1.1 301 Moved Permanently
    Date: Tue, 24 Mar 2020 13:13:00 GMT
    Content-Type: text/html
    Content-Length: 162
    Location: http://domain.com/forums/topic
    Connection: keep-alive
    Server: nginx centminmod
    X-Powered-By: centminmod
    
     
  4. pamamolf

    pamamolf Premium Member Premium Member

    3,991
    416
    83
    May 31, 2014
    Ratings:
    +806
    Local Time:
    3:55 AM
    Nginx-1.17.x
    MariaDB 10.3.x
    Thanks both of you !!

    Don't know why but it works only using:
    Code:
    rewrite ^/forum/(.*)$ https://domain.com/forums/$1 redirect;
    It may not be sufficient at all as if i am not wrong it just creates an extra https request but it's the only one that worked..... don't know why...
     
    Last edited: Mar 24, 2020
  5. eva2000

    eva2000 Administrator Staff Member

    49,299
    11,296
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,575
    Local Time:
    10:55 AM
    Nginx 1.21.x
    MariaDB 10.x
    301 redirects are permanently cached in web browsers, so if you had an incorrect 301 it could still be cached. You should test with 302 first or use private/incognito browser session or like me use curl in SSH to check headers