Search code examples
apache.htaccessmod-rewriteurl-rewritingtrailing-slash

Append '/' after URL using .htaccess


<Directory /var/www/html/>
        Options +SymLinksIfOwnerMatch
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?url=$1 [L]
</Directory>

How can I modify the above .htaccess rule to add a / after the URL but without mucking up the existing RewriteRule?

I tried simply appending: RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301] but this did not work.


Solution

  • Just do a check if the URI ends with a /, and if not add one to whatever the URI was.

    RewriteCond %{REQUEST_URI} !(/$|\.) 
    RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]