Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Cleaning URLs with Rewrite Rules Adding a Trailing Slash

Oziel Perez
Oziel Perez
61,321 Points

Universal Rewrite Rule to add Trailing Slash to Any URL

Ok so after seeing the php video about adding a trailing slash using rewrite rules, I figured it was only applicable to specific urls. What if I have several urls that need to be redirected if there is no trailing slash. Is there something like that out there, especially one that accounts for any depth of directories?

In essence, I want to know if theres a rewrite rule that will, for example, redirect example.com/sample, example.com/anothersample and example.com/sample/sampe-a/sample-b all in one rule.

While I'm at it, an expert suggested that I use this:

RewriteRule ^(([^/]+/)*[^./]+)$ http://example.com/$1/ [R=301, L] 

The rule above didnt work for me in the end.

1 Answer

Zachary Green
Zachary Green
16,359 Points

What would be the end goal of having all those different uri's? you could use the $_SERVER['REQUEST_URI'] super gobal to pull in the uri and parse it. then you could redirect in the php page to the address you want.