• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 1.5 please help nginx rewrite redirect to xf

#1
hello, everyone.
I have a old forum script, I move it to xf 1.5.6
but now the old url can't redirect to new's.
my old url is http://bbs.example.com/forum.php?mod=viewthread&tid=409493&page=1&authorid=84701
and new url is http://bbs.example.com/threads/409493/

I write a nginx rewrite as blow, but it can't work. anyone can help me ?
thanks. @Jake Bunce

location ~* ^/forum.php?mod=viewthread&tid=[0-9]+\.+$ {
rewrite forum.php?mod=viewthread&tid=([0-9]+)&page=[0-9]+&authorid=[0-9]+$ /threads/$1/ permanent;
}
 
Last edited:

Jake Bunce

XenForo moderator
Staff member
#2
Try this:

Code:
        location ~* ^/forum\.php {
                if ($args ~* "mod=viewthread&tid=([0-9]+)(&|$)") {
                        set $tid $1;
                        rewrite ^.*$ /threads/$tid/? last;
                }
        }
My nginx is weaker than my apache, but I am pretty sure this will work.
 

Jake Bunce

XenForo moderator
Staff member
#4
Is that different than without the rules?

I am guessing a little bit, but try any of these in place of the "rewrite" line:

Code:
rewrite ^.*$ /threads/$tid/? permanent;

return 301 /threads/$tid/;

return 301 http://bbs.example.com/threads/$tid/;
 
#5
Is that different than without the rules?

I am guessing a little bit, but try any of these in place of the "rewrite" line:

Code:
rewrite ^.*$ /threads/$tid/? permanent;

return 301 /threads/$tid/;

return 301 http://bbs.example.com/threads/$tid/;
yes, there are the others rewrite rules, blow is the complete configure file:
Code:
server
  {
    listen       80;
    server_name  bbs.example.com www.example.com m.example.com;
    index index.html index.htm index.php;
    root  /home/htdocs/xf;
    if ($host = 'www.example.com' ) {
     rewrite ^/(.*)$ http://bbs.example.com/$1 permanent;
    }
    if ($host = 'm.example.com' ) {
     rewrite ^/(.*)$ http://bbs.example.com/$1 permanent;
    }
    location / {
    try_files $uri $uri/ /index.php?$uri&$args;
    index index.php index.html;
    }

    location ~* ^/forum-[0-9\-]+\.html$ {
                rewrite forum-([0-9]+)\.html$ /forums/$1/ permanent;
                rewrite forum-([0-9]+)-([0-9]+)\.html$ /forums/$1/page-$2 permanent;
        }

    location ~* ^/thread-[0-9\-]+\.html$ {
        rewrite thread-([0-9]+)-([0-9]+)-[0-9]+\.html$ /threads/$1/page-$2 permanent;
          rewrite thread-([0-9]+)-[0-9]+-[0-9]+\.html$ /threads/$1/ permanent;
        }

    location ~* ^/forum\.php {
                if ($args ~* "mod=viewthread&tid=([0-9]+)(&|$)") {
                        set $tid $1;
                        rewrite ^.*$ /threads/$tid/? last;
                }
        }


    location /internal_data/ {
      internal;
    }
    location /library/ {
      internal;
    }

    location ~ [^/]\.php(/|$) {
        #fastcgi_pass remote_php_ip:9000;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        }
 
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      30d;
    }
    location ~ .*\.(js|css)?$
    {
      expires      1h;
    }   
   }
 

Jake Bunce

XenForo moderator
Staff member
#6
I was referring to this line from my original post:

Code:
        location ~* ^/forum\.php {
                if ($args ~* "mod=viewthread&tid=([0-9]+)(&|$)") {
                        set $tid $1;
                        rewrite ^.*$ /threads/$tid/? last;
                }
        }
Try replacing that line with one of these 3 lines:

Code:
rewrite ^.*$ /threads/$tid/? permanent;

return 301 /threads/$tid/;

return 301 http://bbs.example.com/threads/$tid/;
 
#7
I was referring to this line from my original post:

Code:
        location ~* ^/forum\.php {
                if ($args ~* "mod=viewthread&tid=([0-9]+)(&|$)") {
                        set $tid $1;
                        rewrite ^.*$ /threads/$tid/? last;
                }
        }
Try replacing that line with one of these 3 lines:

Code:
rewrite ^.*$ /threads/$tid/? permanent;

return 301 /threads/$tid/;

return 301 http://bbs.example.com/threads/$tid/;
OH, I have replaced this 3 lines, BUT the error as before :
Route forum.php/ could not be found.
this is the complete config file:
Code:
server
  {
    listen       80;
    server_name  bbs.hx317.com www.hx317.com m.hx317.com;
    index index.html index.htm index.php;
    root  /home/htdocs/xf;
    if ($host = 'www.hx317.com' ) {
     rewrite ^/(.*)$ http://bbs.hx317.com/$1 permanent;
    }
    if ($host = 'm.hx317.com' ) {
     rewrite ^/(.*)$ http://bbs.hx317.com/$1 permanent;
    }
    location / {
    try_files $uri $uri/ /index.php?$uri&$args;
    index index.php index.html;
    }

    location ~* ^/forum-[0-9\-]+\.html$ {
                rewrite forum-([0-9]+)\.html$ /forums/$1/ permanent;
                rewrite forum-([0-9]+)-([0-9]+)\.html$ /forums/$1/page-$2 permanent;
        }

    location ~* ^/thread-[0-9\-]+\.html$ {
        rewrite thread-([0-9]+)-([0-9]+)-[0-9]+\.html$ /threads/$1/page-$2 permanent;
          rewrite thread-([0-9]+)-[0-9]+-[0-9]+\.html$ /threads/$1/ permanent;
        }

    location ~* ^/forum\.php {
                if ($args ~* "mod=viewthread&tid=([0-9]+)(&|$)") {
        set $tid $1;
        rewrite ^.*$ /threads/$tid/? permanent;

        return 301 /threads/$tid/;

        return 301 http://bbs.example.com/threads/$tid/;
        }


    location /internal_data/ {
      internal;
    }
    location /library/ {
      internal;
    }

    location ~ [^/]\.php(/|$) {
        #fastcgi_pass remote_php_ip:9000;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      30d;
    }
    location ~ .*\.(js|css)?$
    {
      expires      1h;
    }  
   }