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

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

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

it makes this url;


to this;


which is good but with an url like this;


it makes it;


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

What i am missing?


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 ....

| improve this answer | |
  • Chris thank you. Can you look at my whole code , is it looks correct right now? pastebin.com/DiMc6YtD ALSO where should i add the try files uri 404 in this? thanks. – boranb Nov 15 '11 at 1:05
  • no this is giving me duplicate cause i have already have location php. can you help chris? – boranb Nov 15 '11 at 1:20
  • okat i combined it like that and now everything works fine; pastebin.com/fuKwuCe1 but can you look at my whole code and tell me if i have any security issues? thanks – boranb Nov 15 '11 at 1:28
  • Change line 70 to try_files $uri = 404; See Nginx 0 day exploit for more info. – Chris_O Nov 15 '11 at 1:48
  • thank you very much chris! by the way the site you give is adding 403 not 404. what is the difference? – boranb Nov 15 '11 at 2:15

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.


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


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 ^^

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.