Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save blueprintmrk/a22792b0afab8f8d894b18690d5722a7 to your computer and use it in GitHub Desktop.
Save blueprintmrk/a22792b0afab8f8d894b18690d5722a7 to your computer and use it in GitHub Desktop.
Nginx Rewrite rules WordPress

Rewrite rules for Multisite using subdomains

map $http_host $blogid { default -999;

#Ref: http://wordpress.org/extend/plugins/nginx-helper/
#include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;

}

server { server_name example.com *.example.com ;

root /var/www/example.com/htdocs;
index index.php;

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

location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_pass php;
}

#WPMU Files
    location ~ ^/files/(.*)$ {
            try_files /wp-content/blogs.dir/$blogid/$uri /wp-includes/ms-files.php?file=$1 ;
            access_log off; log_not_found off;      expires max;
    }

#WPMU x-sendfile to avoid php readfile()
location ^~ /blogs.dir {
    internal;
    alias /var/www/example.com/htdocs/wp-content/blogs.dir;
    access_log off;     log_not_found off;      expires max;
}

#add some rules for static content expiry-headers here

}

Note NGINX WordPress Shared Hosting Model - AnsiPress can be used. AnsiPress is created based on Ansible playbooks and support NGINX, PHP7, MariaDB, Google PageSpeed installation as well as automate WordPress installations/setup. For wordpress-nginx based sites management, EasyEngine can be used. EasyEngine (ee) is python based command line control panel to setup NGINX server on Ubuntu and Debian Linux distribution for HTML, PHP, MySQL, HHVM, PageSpeed and WordPress sites. map section can be completed manually for small sites. On large multisite network nginx-helper wordpress plugin can be used. Further performance gain is possible by using NGINX’s fastcgi_cache. When using fastcgi_cache, compile NGINX with ngx_cache_purge module and add a wordpress-plugin which performs automatic cache purge on events e.g. a wordpress post/page is edited. NGINX Cache Controller WordPress plugin provides some functions of controlling NGINX proxy server cache. NGINX Mobile Theme WordPress plugin allows you to switch theme according to the User Agent on the NGINX reverse proxy.

Adding trailing slash to folders only

rewrite ^([^.]*[^/])$ $1/ permanent;

The Regular Expression translates to: "rewrite all URIs without any '.' in them that don't end with a '/' to the URI + '/'" Or simply: "If the URI doesn't have a period and does not end with a slash, add a slash to the end"

The reason for only rewriting URI's without dots in them makes it so any file with a file extension doesn't get rewritten. For example your images, css, javascript, etc and prevent possible redirect loops if using some php framework that does its own rewrites also

Another common rewrite to accompany this would be:

rewrite ^([^.]*)$ /index.php;

This very simply rewrites all URI's that don't have periods in them to your index.php (or whatever file you would execute your controller from).

Non-root try_files to URL redirect
If you want to serve WordPress as a sub directory, you will want to make the following changes (relative to the above configuration).
location /wordpress {
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(/wordpress)(/.*)$;
}

Pre-0.8.30 fastcgi settings¶

If you are using a version below 0.8.30, you will want to add this to your fastcgi_params file.

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

map $uri $blogname{ ~^(?P/[^/]+/)files/(.*) $blogpath ; }

map $blogname $blogid{ default -999;

#Ref: http://wordpress.org/extend/plugins/nginx-helper/
#include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;

}

server { server_name example.com ;

root /var/www/example.com/htdocs;
index index.php;

location ~ ^(/[^/]+/)?files/(.+) {
    try_files /wp-content/blogs.dir/$blogid/files/$2 /wp-includes/ms-files.php?file=$2 ;
    access_log off;     log_not_found off; expires max;
}

#avoid php readfile()
location ^~ /blogs.dir {
    internal;
    alias /var/www/example.com/htdocs/wp-content/blogs.dir ;
    access_log off;     log_not_found off; expires max;
}

if (!-e $request_filename) {
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    rewrite ^(/[^/]+)?(/wp-.*) $2 last;
    rewrite ^(/[^/]+)?(/.*\.php) $2 last;
}

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

location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_pass php;
}

#add some rules for static content expiry-headers here

}

Adding A Trailing Slash

By ThanhJanuary 04, 2011Technology

Some may ask what’s the point (it works as is with or without)? Well, when it comes to Search Engine Optimization, having duplicate may hurt your rankings. Therefore, it’s good practice to have a permanent (301) redirect for one or the other. For me, I prefer adding a trailing slash. And you simply do this by adding a nginx rewrite to your vhost:

# Adds a trailing slash to any urls that is missing a trailing slash
rewrite ^(.*[^/])$ $1/ permanent;

And if you prefer vice versa (urls without trailing slashes), just use the following rewrite instead:

# Remove trailing slash
rewrite ^/(.*)/$ /$1 permanent;
@blueprintmrk
Copy link
Author

# Upstream to abstract backend connection(s) for php
upstream php {
        server unix:/tmp/php-cgi.socket;
        server 127.0.0.1:9000;
}

server {
        ## Your website name goes here.
        server_name domain.tld;
        ## Your only path reference.
        root /var/www/wordpress;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

@blueprintmrk
Copy link
Author

Non-root try_files to URL redirect

If you want to serve WordPress as a sub directory, you will want to make the following changes (relative to the above configuration).

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

location ~ \.php$ {
        fastcgi_split_path_info ^(/wordpress)(/.*)$;
}
Pre-0.8.30 fastcgi settings¶

If you are using a version below 0.8.30, you will want to add this to your fastcgi_params file.

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment