Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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:


<?php

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.