Nginx and WordPress setup clean SEO friendly URLs

Open your nginx configuration file:

# nano /etc/nginx/nginx.conf

Locate your domain (example.com) virtual hosting configuration and before “location ~ \.php$ {” add following lines:

if (!-e $request_filename) {
  rewrite  ^(.*)$  /index.php?q=$1  last;
  break;
}

At the end your configuration should look as follows:

server {
    server_name     example.com;
    index           index.php;
    root            /var/www/example.com;

    if (!-e $request_filename) {
      rewrite  ^(.*)$  /index.php?q=$1  last;
      break;
    }  

    location ~ .php$ {
       fastcgi_pass  localhost:9999;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_NAME $fastcgi_script_name;
       fastcgi_param REQUEST_URI $request_uri;
       fastcgi_param DOCUMENT_URI $document_uri;
       fastcgi_param DOCUMENT_ROOT $document_root;
       fastcgi_param REMOTE_ADDR $remote_addr;
       fastcgi_param REMOTE_PORT $remote_port;
       fastcgi_param SERVER_ADDR $server_addr;
       fastcgi_param SERVER_PORT $server_port;
       fastcgi_param SERVER_NAME $server_name;
       fastcgi_param SCRIPT_FILENAME  /var/www/example.com$fastcgi_script_name;
       fastcgi_param QUERY_STRING $query_string;
       fastcgi_param REQUEST_METHOD $request_method;
       fastcgi_param CONTENT_TYPE $content_type;
       fastcgi_param CONTENT_LENGTH $content_length;
    }
}

Save and close the file. Restart nginx:

# /etc/init.d/nginx restart

Comments 14

  1. Hone wrote:

    How about a permanent redirect for all images in this directory to a different subdomain:

    http://www.domain.com/images/

    to:

    http://images.domain.com/images/

    ?

    Posted 23 Jul 2007 at 7:53 am
  2. Aleksandar wrote:

    Add this line in http://www.domain.com configuration:

    rewrite  ^/images(.*)$  http://images.domain.com/images$1  permanent;
    
    Posted 23 Jul 2007 at 11:43 pm
  3. Gerry Kirk wrote:

    You’ve covered the bases for a lot of configuration needs all in one simple page. Thanks so much!

    Posted 31 Aug 2007 at 4:08 pm
  4. Ales wrote:

    I dunno, but this is the only version of the rewrite that worked for me

    server {
    listen 80;
    charset utf-8;
    server_name xx.xx.com;
    access_log /var/log/nginx/blogi.vest.si.access.log main;
    root /var/www/html/xx.xx.com;
    index index.php;

    # rewrite rule – fancy urls for wordpress mu 1.25 rewrite ^.*/files/(.*) /wp-content/blogs.php?file=$1;
    if (!-e $request_filename) {
    rewrite ^.+?(/wp-.*) $1 last;
    rewrite ^.+?(/.*\.php)$ $1 last;
    rewrite ^(.+)$ /index.php?q=$1 last;
    break;
    }
    …}

    Everything else resulted in “No input file found.”

    Posted 18 Oct 2007 at 3:34 pm
  5. Aleksandar wrote:

    Hi, Ales
    Look again, you might have missed something…
    My configuration is valid. :)

    Posted 19 Oct 2007 at 2:46 am
  6. Aleksandar wrote:

    Ah.. I just noticed, based on your input, you are running wordpress mu..
    This is for wordpress not for wordpress mu.

    Posted 19 Oct 2007 at 2:56 am
  7. Andrew CHalkley wrote:

    Thank you for your help! Worked a treat!

    Posted 26 Apr 2008 at 3:59 pm
  8. G wrote:

    Thanks for your help.

    Posted 18 May 2008 at 5:01 pm
  9. Dilip P wrote:

    this is my config file and i’ve not been able to get it working

    server {

    listen 80;
    server_name http://www.domain.in;
    rewrite ^/(.*) http://domain.in permanent;

    }

    server {

    listen 80;
    server_name domain.in;

    access_log /home/pdilip/public_html/domain.in/log/access.log;
    error_log /home/pdilip/public_html/domain.in/log/error.log;

    location / {
    root /home/pdilip/public_html/domain.in/public/;
    index index.html index.htm index.php;

    }

    location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /home/pdilip/public_html/domain.in/public/$fastcgi_script_name;

    }

    }

    Posted 06 Jul 2008 at 5:06 pm
  10. Emil wrote:

    Thanks, works like charm, or Hvala brate, radi dobro :-)

    Pozdrav iz Cikaga,
    Emil

    Posted 11 Sep 2008 at 6:13 am
  11. Sujay wrote:

    I recently migrated my WP-MU blog from apache to nginx. the blog appears as mydomain.com/blog .
    I am having problems with the nginx rewrite rules. I used the following.

    location /blog/ {
    if (!-e $request_filename) {
    rewrite ^.+?(/.*\.php)$ /blog$1 last;
    rewrite ^.+?(/wp-.*) /blog$1 last;
    rewrite ^ /blog/index.php last;
    }
    }

    The URLS word prrperly but my design is screwed up. Where am I going wrong?

    Posted 13 Aug 2009 at 9:24 pm
  12. Giorgio wrote:

    Hello i’m looking for an experienced System Administrator to set-up Nginx on my VPS account (very trafficked wordpress blog.) since i’m experiencing problems with Apache
    Contact me if you are interested about the small job (and tell me the price you ask) at

    seffignoz at gmail d o t com

    Posted 09 Nov 2009 at 11:24 pm
  13. emcgfx wrote:

    @Sujay, you need to specify sub-directory in all rewrite rules, try this.

    if (!-e $request_filename) {
    rewrite ^.+?(/blog/wp-.*) $1 last;
    rewrite ^.+?(/blog/.*\.php)$ $1 last;
    rewrite ^(.+)$ /blog/index.php?q=$1 last;
    break;
    }

    Posted 05 Apr 2010 at 4:47 am
  14. voku wrote:

    this is for WP3 (multi) not for special MU-Version:

    if (!-e $request_filename) {
    rewrite ^.+/?(/ms-.*) $1 last;
    rewrite ^/files/(.+) /wp-includes/ms-files.php?file=$1 last;
    rewrite ^.+/?(/wp-.*) $1 last;
    rewrite ^.+/?(/.*.php)$ $1 last;
    rewrite ^(.+)$ /index.php?q=$1 last;
    break;
    }

    Posted 04 Aug 2010 at 1:55 pm

Trackbacks & Pingbacks 2

  1. From links for 2008-04-16 « The Inscription on 16 Apr 2008 at 8:41 am

    [...] Aleksandar Savic – Nginx and WordPress setup clean SEO friendly URLs Open your nginx configuration file: (tags: WordPress Nginx) [...]

  2. From Complete Linux PHP MySQL Nginx Reference - VPS Bible #20 - GUVNR on 30 Nov 2009 at 9:03 pm

    [...] Aleksandarsavic.com .. Nginx with WordPress & FURL's [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *