2

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?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Erik van de Ven
  • 4,747
  • 6
  • 38
  • 80

2 Answers2

1

Put this on your .htaccess :

Options +FollowSymlinks
RewriteEngine on

RewriteBase /
RewriteRule ^/test$  /test/ [L]
Mohamed Amine
  • 2,264
  • 1
  • 23
  • 36
  • 1
    Thank you for your quick answer. But 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. – Erik van de Ven Sep 10 '13 at 09:07
  • So post your solution as an answer and accept it, it will help others :) – Mohamed Amine Sep 10 '13 at 09:21
  • Ok I will. But I've to wait for 7 hours -_- : "Users with less than 10 reputation can't answer their own question for 8 hours after asking. You can answer in 7 hours. Until then please use comments, or edit your question instead." – Erik van de Ven Sep 10 '13 at 09:23
  • You have now 13 reputation :) – Mohamed Amine Sep 10 '13 at 09:35
0

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.

Erik van de Ven
  • 4,747
  • 6
  • 38
  • 80