Erik van de Ven
Erik van de Ven

Reputation: 4985

.htaccess if url not ends with extension, add slash at the end

I just started learning regular expressions and I just can't figure this out. I need to force a slash at the end of an URL if it not contains an extensions.

So to be more clear:

example.com/test/ stays the same.
example.com/test.php stays the same.
example.com/test becomes example.com/test/     (See the last slash at the end)

Anyone who knows how to fix this?

Upvotes: 2

Views: 1860

Answers (2)

Erik van de Ven
Erik van de Ven

Reputation: 4985

Strange enough I just found a solution by accident:

RewriteCond %{REQUEST_URI} /+[^\.]+$ 
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

At first it added a slash at the end of every row, even which ended with .php but now it works, strangely enough.

Upvotes: 0

Mohamed Amine
Mohamed Amine

Reputation: 2304

Put this on your .htaccess :

Options +FollowSymlinks
RewriteEngine on

RewriteBase /
RewriteRule ^/test$  /test/ [L]

Upvotes: 1

Related Questions