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 Enhancing a Simple PHP Application Cleaning URLs with Rewrite Rules Introducing Rewrite Rules

Why not use a PHP redirect instead of .htaccess rewrites?

I am having trouble configuring my .htaccess to rewrite the url in the shirts folder, and in the mean time, I thought, "Why not just use a PHP redirect?" I created a file named index.php with:


header('Location: shirts.php'); exit;

and it works perfectly. What is the advantage then of using .htaccess instead?

2 Answers

Richard Duncan
Richard Duncan
5,568 Points

I run wordpress as a CMS with a completely custom front end it's a WP template. One advantage I have found of having the redirect in the .htaccess file is that when WP updates it overwrites the PHP header and replaces it with WP code. Bit of a pain when you want to login and start typing.

Another big advantage of this is if you delete a 'page' or file and want a redirect to a new location, you don't need to update every bit of code that references that location, you can change it once in .htaccess and be assured that it will work from now on.

I'm sure there are lots of other reasons.

That makes sense, thanks for the info! I tried that since I am having issues with my .htaccess, but I will now go back and see how to fix that.