Nginx rewrite rules

I am having some problems with the Nginx rewrite algorithm for WordPress.

I am using this for the rewrite and it works good;

Read More
    server_name www.domain.com domain.com;
    if ($host != 'domain.com') {
    rewrite ^/(.*)     http://domain.com/$1 permanent;
    } 

it makes this url;

http://domain.com/?author=1 

to this;

http://domain.com/author/username/

which is good but with an url like this;

http://domain.com/?author=1&type=like

it makes it;

http://domain.com/author/username/?type=like

and i am not getting any error but the query is not working.

What i am missing?

Related posts

Leave a Reply

2 comments

  1. The correct Nginx rewrite rules for WordPress are:

    location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
        }
    

    This sends everything through index.php and keeps the appended query string intact.

    If your running PHP-FPM you should also add this before your fastcgi_params as a security measure:

    location ~ .php {
            try_files $uri =404;
            
          //  fastcgi_param ....
          //  fastcgi_param ....
          
          fastcgi_pass 127.0.0.1:9000;
    }
    
  2. Please be aware that the & character between $uri ‘&’ $args is very important, as it will partially work without it, but will fail in some cases.

    Correct:

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    

    Wrong:

    location / {
        try_files $uri $uri/ /index.php?q=$uri$args;
    }
    

    The wrong method will manage to handle multiple args:

    https://example.com?myvar=xxx&secvar=xxx // Will work
    

    But fail if only one arg is passed:

    https://example.com?myvar=xxx // Will NOT work
    

    This made it really hard to find the typo for me, but at least I learned something new ^^