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 Rewrite Rules with Query Strings

Rewrite HELP!!!!

My get variable looks like example.com/?s=foo

I added this

RewriteCond %{QUERY_STRING} ^s=([a-z]+)$ RewriteRule ^(.*)$ /%1? [R=301]

which results with a 404 error when I type example.com/?s=foo. It rewrites correctly to example.com/foo but it says it cannot find /foo

To fix this, I tried to add RewriteRule ^([a-z]+)$ /?s=$1 [L]

so that example.com/foo serves example.com/?s=foo but now i have the EVIL INFINITE REDIRECT LOOP. DARN.

I cannot figure this out - up until 4am last night. HELP!@

Hi Adam!

Are you really need 301 redirect from example.com/?s=foo to example.com/foo ? Maybe you want to just pass get parameter to example.com/foo?

If you definitely need 301 redirect, do you have file on a server by /foo URL ?

2 Answers

This is a reply to Alexander. No, I don't need a 301. And there is no page that is a /foo. I want the URL to be clean and read /foo, but I want the PHP file to run /?s=foo (i just don't want the user to see the get parameters - in other words, a "clean url").

Tom Mertz
Tom Mertz
15,254 Points

Maybe try adding a trailing slash at the end of your Rewrite Rule.

So it would read RewriteRule ^(.*)$ /%1/? [R=301]

Hope you already got it working :)