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!

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 .htaccess URL Rewrite

I have a PHP build similar to the one created in the PHP lessons on Treehouse.

I have: - An array of headphones in headphoneArray.php - A list of products from the array in headphones.php - And the product page at headphone.php

Currently, the product page goes to domain.com/headphone.php?id=101 (or whatever the product ID is).

How can I make it so that the page always loads to something like domain.com/headphone/productname ?

1 Answer

My friend gave this to me:

IndexIgnore *
RewriteEngine on
RewriteCond %{THE_REQUEST} \\?u=([^&\\ ]+)($|\\ )
RewriteCond %{THE_REQUEST} \\?x=([^&\\ ]+)($|\\ )
RewriteRule ^ /%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/([^/]+)$ /user.php?u=$1 [L]
RewriteRule ^video/([^/]+)$ /video.php?x=$1 [L]

You will have to change it a bit.