0

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

0

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;
    die();
}

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;

    fastcgi_pass 127.0.0.1:9070;
  }
}

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.

  • Why i need adding it when using NGINX and not have it when using Apache? – Jambul Jakhaia Dec 7 '17 at 10:57
  • You have to add rewrite rules for Apache not Nginx. In Nginx this part already do it for you 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. – kierzniak Dec 7 '17 at 11:04
  • I know about it. But I don't understand why i need WordPress code modification for NGINX. I need NGINX rules solution not PHP code solution. – Jambul Jakhaia Dec 7 '17 at 11:13
  • The WordPress code which I paste is not needed for forward slash to be working. It is just showing that WordPress is redirecting post by his own without any modification with this Nginx configuration. You can remove it. If you get 404 after redirection to post with forward slash this is problem with Nginx configuration or WordPress configuration/theme/plugin. Clean WordPress installation with this nginx configuration is working fine. – kierzniak Dec 7 '17 at 11:17
  • Yes. I'm getting error 404 when redirecting to "/post/" not "/post" – Jambul Jakhaia Dec 7 '17 at 11:21
0

Thry the following: Replace:

try_files $uri $uri/ /index.php?$args;

with :

try_files $uri $uri/ /index.php$is_args$args;

At the nginx config file for your website.

Then at the permalinks (wordpress settings)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.