Search code examples
.htaccessmod-rewritehttp-redirectslashtrailing

How to add a traling slash


I have the follow code in my htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/?$ index.php?lang=$1
RewriteRule  ^([a-z]{2})/([a-z]+)$  $2.php?lang=$1

How I add a trailing slash, so I can use http://www.liveandletdive.fi/en/contact/ instead of only http://www.liveandletdive.fi/en/contact ?

I figured out, how I can do my site with seo freindly urls, and multilingual. Only this small part remain.


Solution

  • You can use these rules:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    
    ## Adding a trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
    RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    RewriteRule ^([a-z]{2})/?$ index.php?lang=$1 [L,QSA,NC]
    RewriteRule ^([a-z]{2})/([a-z]+)/?$  $2.php?lang=$1 [L,QSA,NC]