XF 2.2 Old apache redirects ---> Nginx redirects

stanleyb23

Member
I have moved my forum from apache to nginx. It's working great but there's one thing i d like to fix.
My old rewrite rules are not working now. There are still a lot of extarnal links to my forum and they get 404's now.

What's the best way to solve this?

Thanks for your help!!
Ralf

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    # MattW
    RewriteRule ^about([0-9]+)\.html /Forum/topic/$1/ [L,R=301,NC]
    RewriteRule ^(.*)\-t([0-9]+)\.html /Forum/topic/$2/ [L,R=301,NC]
    RewriteRule ^(.*)\-t([0-9]+)\-([0-9]+)\.html /Forum/topic/$2/ [L,R=301,NC]
    RewriteRule ^(.*)\-f([0-9]+)\.html /Forum/forum/$2/ [L,R=301,NC]
    RewriteRule ^(.*)\-f([0-9]+)\-([0-9]+)\.html /Forum/forum/$2/ [L,R=301,NC]
    RewriteRule ^ntopic([0-9]+)\.html /Forum/topic/$1/ [L,R=301,NC]
    RewriteRule ^ptopic([0-9]+)\.html /Forum/topic/$1/ [L,R=301,NC]
    RewriteRule ^post([0-9]+)\.html /Forum/posts/$1/ [L,R=301,NC]
    RewriteRule ^post\-([0-9]+)\.html /Forum/posts/$1/ [L,R=301,NC]
    RewriteRule ^about([0-9]+)\-([0-9)+)\-asc\-([0-9]+).html /Forum/topic/$1/ [L,R=301,NC]
    RewriteRule ^posting\.php$ /Forum/? [L,R=301,NC]
    # End MattW
    RewriteCond %{QUERY_STRING} (^|&)t=([0-9]+)(&|$) [NC]
    RewriteRule rss\.php https://www.klusidee.nl/Forum/topic/%2/ [R=301,L]
    RewriteCond %{QUERY_STRING} (^|&)t=([0-9]+)(&|$) [NC]
    RewriteRule ^viewtopic\.php$ /Forum/topic/%2/? [L,R=301,NC]
    RewriteCond %{QUERY_STRING} f=(\d+)$ [NC]
    RewriteRule ^(viewforum\.php|viewtopic\.php)$ /Forum/forum/%1/? [L,R=301,NC]
    RewriteCond %{QUERY_STRING} (^|&)p=([0-9]+)(&|$) [NC]
    RewriteRule ^viewtopic\.php$ /Forum/posts/%2/? [L,R=301,NC]
    RewriteCond %{QUERY_STRING} u=(\d+)$ [NC]
    RewriteRule ^memberlist\.php$ /Forum/members/%1/? [L,R=301,NC]
    RewriteCond %{QUERY_STRING} search_author=(.*)$ [NC]
    RewriteRule ^search\.php$ /Forum/search/?q=%1 [L,R=301,NC]
    RewriteCond %{QUERY_STRING} searchword=(.*)$ [NC]
    RewriteRule ^search\.php$ /Forum/search/?q=%1 [L,R=301,NC]

    #    If you are having problems with the rewrite rules, remove the "#" from the
    #    line that begins "RewriteBase" below. You will also have to change the path
    #    of the rewrite to reflect the path to your XenForo installation.
    #RewriteBase /Forum

    #    This line may be needed to workaround HTTP Basic auth issues when using PHP as a CGI.
    #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
 
chatgpt.com is your friend...

Here is a good start.

Code:
server {
    listen 80;
    server_name yourdomain.com;
    
    # Redirect HTTP to HTTPS
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }

    # MattW rewrite rules
    rewrite ^/about([0-9]+)\.html /Forum/topic/$1/ permanent;
    rewrite ^/(.*)\-t([0-9]+)\.html /Forum/topic/$2/ permanent;
    rewrite ^/(.*)\-t([0-9]+)\-([0-9]+)\.html /Forum/topic/$2/ permanent;
    rewrite ^/(.*)\-f([0-9]+)\.html /Forum/forum/$2/ permanent;
    rewrite ^/(.*)\-f([0-9]+)\-([0-9]+)\.html /Forum/forum/$2/ permanent;
    rewrite ^/ntopic([0-9]+)\.html /Forum/topic/$1/ permanent;
    rewrite ^/ptopic([0-9]+)\.html /Forum/topic/$1/ permanent;
    rewrite ^/post([0-9]+)\.html /Forum/posts/$1/ permanent;
    rewrite ^/post\-([0-9]+)\.html /Forum/posts/$1/ permanent;
    rewrite ^/about([0-9]+)\-([0-9]+)\-asc\-([0-9]+)\.html /Forum/topic/$1/ permanent;
    rewrite ^/posting\.php$ /Forum/? permanent;

    # Query string rewrite rules
    if ($arg_t ~ "([0-9]+)") {
        rewrite ^/rss\.php$ https://www.klusidee.nl/Forum/topic/$arg_t/ permanent;
        rewrite ^/viewtopic\.php$ /Forum/topic/$arg_t/ permanent;
    }

    if ($arg_f ~ "(\d+)") {
        rewrite ^/(viewforum\.php|viewtopic\.php)$ /Forum/forum/$arg_f/ permanent;
    }

    if ($arg_p ~ "([0-9]+)") {
        rewrite ^/viewtopic\.php$ /Forum/posts/$arg_p/ permanent;
    }

    if ($arg_u ~ "(\d+)") {
        rewrite ^/memberlist\.php$ /Forum/members/$arg_u/ permanent;
    }

    if ($arg_search_author) {
        rewrite ^/search\.php$ /Forum/search/?q=$arg_search_author permanent;
    }

    if ($arg_searchword) {
        rewrite ^/search\.php$ /Forum/search/?q=$arg_searchword permanent;
    }

    # Static files rule
    location ~* ^/(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt)$ {
        break;
    }

    # Serve existing files
    location / {
        try_files $uri $uri/ /index.php;
    }
    
    # Optional: Handle PHP with FastCGI (assuming PHP-FPM is set up)
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;  # Adjust as necessary
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
 
Back
Top Bottom