WordPress and NGINX - permalinks are not working with ending slash

by Jambul Jakhaia   Last Updated December 07, 2017 11:08 AM

I have added this line in the nginx configuration file

try_files $uri $uri/ /index.php?$args;
rewrite ^([^.]*[^/])$ $1/ permanent; #adding slash

and the permalinks are working fine, but when adding / (please see rewrite rule how i'm adding it) NGINX gives me 404 error.

For example somedomain.com/post/ThisIsPermaLink (working) after adding slash somedomain.com/post/ThisIsPermaLink/ (gives 404 error)

But all this two versions are working on Apache environment. Apache is redirecting all requests without slashes (somedomain.com/post/ThisIsPermaLink) to somedomain.com/post/ThisIsPermaLink/

So i need same functionality with NGINX

Answers 1

I don't think you need this rewrite rule to add forward slash. WordPress will make redirect for you.

I'm using this code to debug redirection:

function wpse_287994_debug_redirect( $url ) {

    echo $url;

add_action( 'wp_redirect', 'wpse_287994_debug_redirect' );

When I'm trying to visit address http://example.com/lorem-ipsum (this page must exists) above script will output http://example.com/lorem-ipsum/ so redirection to address with forward slash is working.

My ngnix configuration looks like:

server {
  listen *:80;
  server_name           example.com;
  root                  /var/www/example;

  access_log            /var/log/nginx/access.log;
  error_log             /var/log/nginx/error.log;

  index index.php index.html;

  location / {
    # try to serve file directly, fallback to index.php
    try_files $uri $uri/ /index.php?$args;

  location ~ \.php$ {

    include fastcgi.conf;


In Nginx rewrites are already made by this part try_files $uri $uri/ /index.php?$args;. This part mean something like that.

For location beginning with / character try to load file from file system and if there is no such a file rewrite to index.php with arguments.

December 07, 2017 10:53 AM

Related Questions

Correct Nginx configuration with php-fpm

Updated June 02, 2015 22:03 PM

nginx - permalinks with .php in url not working

Updated June 07, 2015 23:03 PM

Some permalinks are not working and show 404

Updated May 29, 2015 22:03 PM

Nginx 404 error with permalinks

Updated March 05, 2016 01:03 AM