E
E
ekopach2020-05-13 20:25:23
Nginx
ekopach, 2020-05-13 20:25:23

How to remove redirect from /blog to /blog/ (slash at the end) in wordpress using nginx?

There is a site where all requests to /blog are proxied by nginx to php-fpm.
Wordpress is unpacked in a separate directory. nginx config (section handling /blog requests):

location @wp {
  rewrite ^/blog(.*) /blog/index.php?q=$1;
}
  
location /blog {           
  alias /var/www/blog;
  index index.php;
  
  try_files $uri $uri/ @wp;
    
  location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
  }
    
  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    access_log    off;
    log_not_found    off;
    expires 1M;
  }
  
  location ~ \.php$ {
                if_modified_since off;
           
    fastcgi_pass php-fpm;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_ignore_client_abort off;
                fastcgi_pass_header Last-Modified;
  }
}

The problem is that for a request to site.ru/blog, nginx returns a 301 redirect to site.ru/blog/ with a slash at the end.
The request is precisely processed by nginx and adds a redirect, the request does not reach wordpress - I checked it.
Can you please tell me what is wrong with the config? How to remove the redirect so that the page opens with and without a slash at the end.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
ekopach, 2020-05-14
@ekopach

Figured it out myself, maybe it will help someone:
1. In the nginx config, change: To: After that, both /blog and /blog/ will work - i.e. this removes the redirect on the nginx side. BUT, it turns out there is another redirect in Wordpress, exactly the same, which can be turned off by entering in functions.php:
try_files $uri $uri/ @wp;
try_files $uri @wp;

remove_filter('template_redirect', 'redirect_canonical');

In this case, the functionality of canonical redirects will be completely disabled.
I used the following code to disable the redirect for the main blog page only:
add_filter('redirect_canonical', 'homepage_disable_redirect_canonical');
function homepage_disable_redirect_canonical($redirectUrl) {
  if(is_home()) {
        $redirectUrl = false;
    }

  return $redirectUrl;
}

I corrected the nginx config - it didn’t help, I added filters to function.php - it didn’t help, but I had to make changes in the complex.

O
Orkhan Hasanli, 2020-05-13
@azerphoenix

Hello!
Try here, these solutions -
https://serverfault.com/questions/597302/removing-...
Google

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question