This tutorial explains how to rewrite urls having a trailing slash to an url without the trailing slash.
Apache
Using Apache we have two options, set the rewrites in a .htaccess (slower) file or in the vhost configuration.
.htaccess
Add the highlighted lines to your .htaccess file, keep in mind that the position is important. I advice you to keep it in the same order as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . /index.php [L] </IfModule> |
vhost configuration
In a vhost configuration it works almost identical as the .htaccess file, same rule as above, keep the order correctly to make it function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<VirtualHost 127.0.0.1:80> ServerName my.server DocumentRoot /var/www/html ServerAdmin webmaster@localhost <IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . /index.php [L] </IfModule> </VirtualHost> |
Nginx
In Nginx we have one type of configuring, in the vhost config. This is the webserver I prefer most.
Add the highlighted lines to your server block to rewrite the urls.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
server { root html/projects/project/public; if (!-d $realpath_root/$uri) { rewrite ^/(.*)/$ /$1 permanent; } try_files $uri $uri/ /index.php?$args; location ~ \.php$ { try_files $uri =404; include fastcgi_params; fastcgi_pass unix:/var/run/php-fpm/pool.sock; fastcgi_read_timeout 300s; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; } } |