Want more timely Centmin Mod News Updates?
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,737
    362
    83
    May 31, 2014
    Ratings:
    +696
    Local Time:
    11:11 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

    58
    13
    8
    Feb 22, 2015
    Ratings:
    +37
    Local Time:
    11:11 AM
    Nginx 1.17.x
    MariaDB 10.3.x
    Code:
    location = /forum {
      return 301 /forums;
    }
    
    or
    Code:
    location ~ /forum/(.*)$ {
       rewrite ^ /forums/$1?$args permanent;
    }
    
     
    • Informative Informative x 1
  3. eva2000

    eva2000 Administrator Staff Member

    43,579
    9,884
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +15,264
    Local Time:
    6:11 PM
    Nginx 1.17.x
    MariaDB 5.5/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
    
     
    • Informative Informative x 1
  4. pamamolf

    pamamolf Premium Member Premium Member

    3,737
    362
    83
    May 31, 2014
    Ratings:
    +696
    Local Time:
    11:11 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
    • Like Like x 1
  5. eva2000

    eva2000 Administrator Staff Member

    43,579
    9,884
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +15,264
    Local Time:
    6:11 PM
    Nginx 1.17.x
    MariaDB 5.5/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
     
    • Informative Informative x 1