How to fix WordPress permalinks in Nginx

WordPress is blazing fast in Nginx. The only problem is the permalinks structure.

nginx_wordpress_permalinks

Unlink Apache, with mod_rewrite builtin, Nginx has no module like that. Because WordPress cant detect the module, it will fallback to PATHINFO directive, forcing an “index.php” on your links. You have to add the “rules” manually if you want to remove that and use the custom structure successfully.

Steps

Log on your root terminal, perform (assuming you have sitename.com)

sudo pico /etc/nginx/sites-enabled/sitename.com.conf

Add / Edit the following line:

server {
server_name www.sitename.com;
rewrite ^(.*) http://sitename.com$1 permanent;
}

server {
server_name sitename.com;
root /var/www/sitename.com;
include /etc/nginx/fastcgi_php;
location / {
index index.php;
if (!-e $request_filename) {
# rewrite ^(.*)$ /index.php last;
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
}

You can of course use FTP if you wish. But you still need root access to the terminal to make the changes take effect:

nginx -s reload

Leave a Reply

Your email address will not be published. Required fields are marked *