How to Rewrite a Trailing Slash for SEO Purposes

Search engines don’t like duplicate content. To rewrite trailing slashes some additional Nginx configuration is required.

If you rewrite all URLs, some issues may occur with the Magento backend admin and some other endpoints.

To resolve this issue, use the following snippet to strip all trailing slashes and add this to your /data/web/nginx/server.rewrites:

if ($uri !~ /admin|/webpos) {
    rewrite ^/(.*)/$ /$1 permanent;
}

Or this snippet if you want to add trailing slashes:

if ($uri !~ /admin|/webpos) {
    rewrite ^/(.*)/$ /$1/ permanent;
}

Replace /admin and /webpos with URLs that should not be rewritten in a regex of URLs separated by pipes (|).