PHP’s Header function can come in quite handy when you’re building your next greate web application. Its powerful, but as a result, its tempting to misuse it to do even the simplest things, like permanent redirects. Usually, its done like this:
// redirect /publications (this page) to real page (/documents)<br />Header("Location: /documents/");<br />
One line of code, time to move on to the next task in your queue, right? No. Why is this wrong or wasteful? We’re making the web server fire up the PHP processor to do something it can do more efficiently. If we avoid using PHP, our request should be served faster and with less memory usage. If you’re on Apache, then the Rewrite module can do this for you, assuming your hosting provider has it enabled and allows you to control it via .htaccess files. The better way to do this is with the following:
RewriteEngine On<br />RewriteRule /publications/? /documents/ [R=permanent, L]<br />
The "permanent" part is important, in that it’ll trigger well-behaved spiders and user-agents, like Google, to update their link database and avoid the redirect in the future altogether. If you have a lot of redirects, this solution excels for its maintainability as well. Keeping them all in a single .htaccess file will save you from having to hunt through multiple PHP files for that header() call.