MZON
MZON

Reputation: 305

htaccess redirect trailing slash

I'm trying to redirect url's so if the url is http://foobar.com/admin/bla to be redirected to http://foobar.com/admin/bla/

Here is the content of .htaccess file

RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|assets|stylesheets|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]

The problem is that the redirection doesn't work. When I type http://foobar.com/admin/bla the response is 200. What I want is to be 301 redirected to the url with the / at the end What I'm doing wrong?

Upvotes: 1

Views: 84

Answers (1)

Justin Iurman
Justin Iurman

Reputation: 19026

This should work instead

RewriteEngine on
RewriteBase /

# if it's an existing folder or file, do nothing
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]

# if no trailing slash then add it
RewriteRule ^(.+)([^/])$ $1$2/ [L,R=301]

# internally rewrite url to index.php controller
RewriteRule ^(.*)$ index.php/$1 [L]

Upvotes: 1

Related Questions