16 responses to “WordPress NGINX Rewrite Rules”

  1. Dimitris Oupas

    Thanks!

    Finally rules that work 🙂

  2. Brad Proctor

    For www to non-www, you should set up a second virtual server

    server {
       server_name www.example.com;
       rewrite ^ http://example.com$request_uri? permanent;
    }
    
    server {
       server_name example.com;
       ...
    }
    

    Also, instead of the

    if (!-e $request_filename)

    segment, you should use try_files

    location / {
       try_files $uri $uri/ /index.php
    }
    
  3. mattonomics

    Out of curiosity, are you familiar with an nginx/WordPress problem where rewritten pages (e.g. site.com/%postname%/) cannot properly proces $_GET vars? I’m running into this issue.

    For the url site.com/page/?foo=bar&blue=smurf, print_r($_GET) is outputting q=>page and nothing else!

  4. Ovidiu

    just wondering why on my test site, in the permalinks settings wordpress seems to like to place an index.php in-between? it all works fine without…

    http://www.diigo.com/item/image/1eygw/ois4

  5. Fadlee

    @Ovidiu, you can use this plugin fix that trouble:

    http://wordpress.org/extend/plugins/nginx-compatibility/

  6. Ovidiu

    @Fadlee: thanks, worked perfectly 🙂

  7. Dhruv

    Hey Dimas,

    Any tips on combining WordPress AND CodeIgniter? I am running a WP install in the root folder (with this config, a modified version of the Roots WP Theme config: here). However, I’m also trying to migrate a CI app into a /subdirectory/ on my site. I’ve managed to get the default controller to show (without the index.php), but any other controllers or methods simply break and show my WP 404 page! HIstorically, the CI app used to run on an Apache server with pretty URL’s just fine.

    Any help is appreciated 😀

  8. Dhruv

    I managed to sort it out after tweaking a few settings. 🙂 My config is available here.

  9. chrismccoy

    you mind posting your nginx.conf?

    im having issues getting mine working

  10. RavanH

    Hi,

    I found that the rule
    rewrite ^(.+)$ /index.php?q=$1 last;
    adds a 'q' request parameter behind the scenes and although invisible for visitors browsing the site, it is seen by WP Super Cache in PHP mode. This results in no pages are handled by Super Cache because all requests with a GET parameter are being passed to WP Cache instead. Except the home page, that is...

    Instead, the rule
    rewrite ^(.+)$ /index.php last;
    seems to work equally well for WordPress and makes WP Super Cache behave nice on all posts and pages too :)

  11. Al Jachimiak

    Awesome post!

    I just put together an ubuntu server with Nginx and I could figure out what the issue was.

    Thanks!

  12. dydyt

    WORKS! Thank You 🙂

  13. John Wheal

    Thanks for this post. I’m trying to automate this rewrite tool to work with ispconfig.

  14. Adrian

    Thanks for the post!

    I set up a virtual host so that http://www.domain.com is rewritten to domain.com, that way the IF statement in the article is not needed.

    Here is my complete virtual host file (rewrites http://www.domain.com to domain.com and has pretty URL’s enabled):

    http://pastebin.com/v38KG0Mx

    Please note that I am using a PHP port instead of a socket.

  15. denellum

    Been beating my head against the wall ALL day trying to figure out how to clean up the URLS with nginx. Thank you so so so much for this 🙂