Nginx – Add trailing slash when it’s missing in nginx

magentonginxrewriteurl

I'm running Magento on Nginx using this config: http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/configuring_nginx_for_magento.

Now I want to 301 all URLs without trailing slash to their counterpart that includes a trailing slash. For example: /contacts to /contacts/.

I've tried virtually all the nginx directives on this I could find, but to no avail. For example, the directive specified in nginx- Rewrite URL with Trailing Slash leads to a redirect to /index.php/.

Which directive should I add and where?

Best Answer

I've found the solution: I added the following line above the "try_files" directive in the "location /"-block:

rewrite ^([^.]*[^/])$ $1/ permanent;

which does the magic.