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

.htaccess help

I'm trying to get rid of file extensions on my website. So far I have some code to get rid of any .php extensions in the main directory:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ $1.php

However, I can't figure out how to get this to work when a url is not in the main directory. I have a user system that works using a get variable using the user's id, for example, profile.php?user_id=1

I want to have it so that when the url is /u/1 it goes to that. Is that possible using .htaccess?

6 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Yes, you can. The rule would need to look something like this:

RewriteRule ^u/([0-9]+)$ /profile.php?user_id=$1

(I'll be talking about Rewrite Rules just like this in the set PHP of videos coming out in a few weeks: Roadmap.)

Does that help?

Google informed me of this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

source: http://blog.sicanstudios.com/post/how-to-remove-php-html-htm-extensions-with-htaccess

Thanks! This did exactly what I wanted! And I can't believe a treehouse teacher actually replied to me!

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

@Matthew, Glad that worked for you! :~)

does anyone know how to do a 301 redirect with Yahoo as the hosting company? My goal is to have both the www and non www version of a site go to the www version.

Joel -

Friendly Mod Note :tm:

This doesn't appear to be related to the original question posed by Matthew, except in the vague way that they are both help on the topic of .htaccess.

Remember the rule of thumb: your issue, your thread

Please create your own thread on the topic of 301 redirects and I or another forum member will be glad to dicuss it in that thread.