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

Rewrite Rules - Apache to Nginx

Discussion in 'Server Configuration and Hosting' started by Fred., Feb 11, 2015.

  1. Fred.

    Fred. Well-Known Member


    Can someone please help me out. I'm trying to find this out for 3 days now :confused:
    I'm moving from Apache to Nginx.

    These are my apache rewrite rules.

    # vBSEO to XF
    RewriteEngine on
    RewriteCond %{SERVER_PORT} 80
        RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} !^www\.example\.com$
        RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} ^example.com$
    RewriteRule (.*) https://www.example.com$1 [R=301,L]
    RewriteRule [^/]+/([\d]+)-.+-([\d]+).html showthread.php?t=$1&page=$2 [NC,L]
    RewriteRule [^/]+/([\d]+)-.+.html showthread.php?t=$1 [NC,L]
    RewriteRule ^members/([^\.]+)\.html$ /member_redirect.php?username=$1 [R=301,L]
    RewriteRule ^algemeen/$ /forums/algemeen.29/ [R=301,L]
    <IfModule mod_rewrite.c>
        RewriteEngine On
        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]
    And this is what I have for nginx.
    Xenforo is working but my old vbulletin url's are not. The url redirection scripts are in place.
    I get
    Route algemeen/6349-thread-title.html could not be found.

    rewrite ^(.*)$ https://$server_name$1 permanent;
    location / {
        try_files $uri $uri/ /index.php?$uri&$args;
        index index.php index.html;
    location /internal_data/ {
    location /library/ {
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    # vBulletin-VBSEO to XenForo RR
    rewrite /[^/]+/([d]+)-.+-([d]+).html /showthread.php?t=$1&page=$2 last;
        rewrite /[^/]+/([d]+)-.+.html /showthread.php?t=$1 last;
        rewrite ^/members/([^.]+).html$ /member_redirect.php?username=$1 permanent;
        rewrite ^/algemeen/$ /forums/algemeen.29/ permanent;
    Thanks! :)
  2. Fred.

    Fred. Well-Known Member

    I think I got it...
    The threads are working now.
    I replaced these rules
    rewrite /[^/]+/([d]+)-.+-([d]+).html /showthread.php?t=$1&page=$2 last;
        rewrite /[^/]+/([d]+)-.+.html /showthread.php?t=$1 last;
    by this
            rewrite [^/]+/([0-9]+)-.+-([0-9]+)\.html /showthread.php?t=$1&page=$2 last;
        rewrite [^/]+/([0-9]+)-.+\.html /showthread.php?t=$1 last;
    The only thing I see now is that thread / forum paging isn't working. And is seems like it wasn't working with my apache rules ether :eek:
    Does anyone know how I can fix this?
  3. Tower

    Tower Active Member

    # redirect from non-www to www
    # uncomment, save file and restart Nginx to enable
    #server {
    #            listen   80;
    #            server_name example.com;
    #            return 301 $scheme://www.example.com$request_uri;
    #       }
    server {
      server_name example.com www.example.com;
    # ngx_pagespeed & ngx_pagespeed handler
    #include /usr/local/nginx/conf/pagespeed.conf;
    #include /usr/local/nginx/conf/pagespeedhandler.conf;
    #include /usr/local/nginx/conf/pagespeedstatslog.conf;
      # limit_conn limit_per_ip 16;
      # ssi  on;
      access_log /home/nginx/domains/example.com/log/access.log combined buffer=32k;
      error_log /home/nginx/domains/example.com/log/error.log;
      root /home/nginx/domains/example.com/public;
       location /forums/ {
                index index.php index.html index.htm;
                try_files $uri $uri/ /forums/index.php?$uri&$args;
            location /forums/internal_data/ {
            deny all;
            location /forums/library/ {
            deny all;
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/php.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
    This is what I use WITHOUT a forums directory
  4. Tower

    Tower Active Member

    Just fill in the example portion with your site address and that should work :)

Share This Page