Close

March 21, 2019

NGINX Removing the Trailing Slash in the URL

For SEO purposes, you always want to make sure that the canonical url always is consistent with the url. So if you allow both a URL with and without the trailing slash, you need to make sure that your canonical is the same for both.

We also want to make sure that the URL is properly redirected as well, just to avoid any inconsistencies with other search engines.  Surprisingly we found it difficult to do with both Magento and NGINX in an efficient manner.  You also have to make sure that if you remove/redirect based on the trailing slash, you have to bypass it on certain applications within Magento.  Magento likes to use that on some of the API processes.

Here is a snippet that we used to do the redirect.  You would just need to augment the bypass list based on your setup.

 # 31819 Redirect /foobar/ to /foobar
        if ($request_method = GET) {
            set $test  P;
        }
        if ($uri !~ /(checkout|customer|authnetcim|sales|review|wishlist|oath|reward)/(.+) ) {
            set $test  "${test}C";
        }
        if ($test = PC) {
            rewrite ^(.+)/$ $1 permanent;
        }