Andrea Dorigo
Andrea Dorigo

Reputation: 165

add .htaccess trailing slash

I'd like to put a trailing slash to my url with .htaccess just in these 3 cases:

  1. The part after the last slash doesn't contain a ? char
  2. The part after the last slash doesn't contain a . char
  3. The last url char is not a / char

I'd like to add those rules to my existing .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.* - [NC,L]
RewriteRule ^.* index.php [NC,L]

Upvotes: 1

Views: 299

Answers (2)

Gerben
Gerben

Reputation: 16825

RewriteCond %{QUERY_STRING} ^$    [NC]
RewriteRule ^((.*/)?[^/\.]+)$ /$1/ [R,L]

Upvotes: 3

dambrisco
dambrisco

Reputation: 865

RewriteRule ^(.*?[^/\?\.])$ http://example.com/$1/ [L]

Should do what you're looking for. Just add it right below RewriteEngine On in your existing file.

Upvotes: 0

Related Questions