Enable WordPress’s permalinks with NGINX

Enable WordPress’s permalinks with NGINX

By default, WordPress will have links that aren’t very pretty, ie:  : http://lucraymond.net/?p=28

It’s not really WordPress’s fault, these kinds of links will work on any hosting platforms with PHP enabled. In order to enable prettier links, you need to enable a feature enabled MOD_REWRITE (in Apache) and you can do it natively with NGINX.

You need to edit the configuration file for your web site. You should see the following:

server {
... some stuff here
}

INSERT THE CODE BELOW HERE

location ~ \.php$ {
… some stuff here
}

Insert the following code, as indicated above and save your configuration file

if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# unless the request is for a valid file, send to bootstrap
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
# catch all
error_page 404 /index.php;

After this is done, restart NGINX.

service nginx restart

Next, enable permalinks in WordPress:

permalinks-1

Next, enable the desired link type:

permalinks-2

Click save and test your website

Leave a Reply