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?

2 Answers 2

1

Put this on your .htaccess :

Options +FollowSymlinks RewriteEngine on RewriteBase / RewriteRule ^/test$ /test/ [L] 
Sign up to request clarification or add additional context in comments.

4 Comments

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.
So post your solution as an answer and accept it, it will help others :)
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."
You have now 13 reputation :)
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.