With some help from @jon-lin, we figured out how to remove trailing slashes on Apache (Vanity URLs without trailing slashes on Apache); 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 -- rewrite ^/(.*)/$ https://www.example.com/$1 permanent;
rewrite ^/(.*)/$ https://www.example.com/$1;
and rewrite ^/(.*)/$ /$1 permanent;
-- commented out) is:
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;
}
How can this be done?
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:
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/
Then, rewriting all entries in /profiles/
was accomplished with this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /profiles/$1 [NC,L]