Nginx – Removing trailing slashes at an nginx subdirectory

nginxWordpress

I have a Symfony application at mysite.com and a WordPress application at mysite.com/blog:

server {
    server_name mysite.com;
    root /var/www/mysite/symfony/web;

    location / {
        try_files $uri /app.php$is_args$args;
    }

    location ~ ^/app\.php(/|$) {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        internal;
    }

    location /blog {
        root /var/www/mysite/wordpress;
        rewrite ^/blog/(.+)$ /$1 break;
        try_files $uri $uri/ /blog/index.php$is_args$args;

        location ~ \.php {
            fastcgi_pass 127.0.0.1:9000;
            include fastcgi_params;
            fastcgi_split_path_info ^(?:\/blog\/)(.+\.php)(.*);
            fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        }
    }

    access_log /var/log/nginx/mysite.com-access.log;
    error_log /var/log/nginx/mysite.com-error.log;
}

When I access my blog route (mysite.com/blog), nginx 301 redirects to add a trailing slash (mysite.com/blog/). Is there any way I can avoid this redirect? I've noticed this doesn't happen with Symfony – mysite.com/admin works fine.

I've tried adding rewrite ^/(.*)/$ /$1;, but this just results in a redirect loop.

Best Answer

The trailing slash is added by WordPress. You can disable it by going to your WordPress Settings -> Permalinks, choose custom structure and remove the trailing slash.