title: Vanity URLs without trailing slashes on Nginx
body: <p>With some help from @jon-lin, we figured out how to remove trailing slashes on Apache (<a href="https://stackoverflow.com/questions/33228428/vanity-urls-without-trailing-slashes-on-apache">Vanity URLs without trailing slashes on Apache</a>); now, as we plan to run our site on Nginx, we'd like to do the same. We tried several suggestions we found on stackoverflow and elsewhere, but all result in an infinite loop (probably because browsers try to put the slashes back in). Our current configuration (with three of our attempts -- <code>rewrite ^/(.*)/$ https://www.example.com/$1 permanent;</code> <code>rewrite ^/(.*)/$ https://www.example.com/$1;</code> and <code>rewrite ^/(.*)/$ /$1 permanent;</code> -- commented out) is:</p>
  
  <pre><code>server {
          listen 80;
          server_name  example.com;
          return 301 https://www.example.com$request_uri;
  }
  server {
          listen 80;
          server_name  www.example.com;
          return 301 https://www.example.com$request_uri;
  }
  server {
          listen 443;
          server_name  example.com;
          return 301 https://www.example.com$request_uri;
  }
  
  server {
          listen 443 ssl default_server;
          listen [::]:443 ssl default_server;
          server_name www.example.com;
  
          #rewrite ^/(.*)/$ https://www.example.com/$1 permanent;
          #rewrite ^/(.*)/$ https://www.example.com/$1;
          #rewrite ^/(.*)/$ /$1 permanent;
  
          root /var/www/example.com/html;
          index index.php index.html index.htm;
  
          location / {
                  # First attempt to serve request as file, then
                  # as directory, then fall back to displaying a 404.
                  try_files $uri $uri/ =404;
          }
  
          location ~ \.php$ {
                  include snippets/fastcgi-php.conf;
                  fastcgi_pass unix:/var/run/php5-fpm.sock;
          }
  
          ssl on;
          ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
          ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
          ssl_prefer_server_ciphers on;
          ssl_session_cache shared:SSL:10m;
          ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
          ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECD$
          add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
          ssl_dhparam /etc/ssl/certs/dhparam.pem;
  
  }
  </code></pre>
  
  <p>How can this be done?</p>
  
  <p>In our particular case, we needed all URLs to be stripped of their trailing slashes; we accomplished that by adding the following to Apache's httpd.conf:</p>
  
  <pre><code>DirectorySlash Off
  
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^(.*[^/])$ /$1/
  </code></pre>
  
  <p>Then, rewriting all entries in <code>/profiles/</code> was accomplished with this:</p>
  
  <pre><code>RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ /profiles/$1 [NC,L]
  </code></pre>