Note: I assume that by "redirect" you
mean a rewrite rule which doesn't
actually use the [R] flag, but instead
"redirects" a request to a different
PHP file - e.g. with no flags at all
or an [L] flag.
If this assumption is wrong, then please have a look at my "Note on performance" argument, which still holds.
.htaccess rule will definitely be faster!
Here's why:
- using .htaccess rule: when a request comes in to Apache, it will check the .htaccess to see if anything needs to be done with that request; noticing a rewrite rule, it will re-route the request to PHP, and site visitor will immediately get the final result
- using PHP
header: request comes in, is processed by Apache, then given to PHP, then PHP sends to your visitor's browser a page-redirect header, browser sends another request, Apache processes it, gives it to PHP, and only after all that your visitor finally gets what he wanted!
Note on performance: in any scenario (even on a gigabit LAN) option #1 above will be faster. Regular expressions might be slow, but Apache is a C program already in memory, and PHP is an interpreted language, which has to read and load the script file before processing it. Thus, I would actually expect better performance from Apache's rewrite rule. The major factor, though, is an extra HTTP response-request for method #2.
If you do insist on doing it with PHP, then consider this:
- cache all the output you generate in your PHP app
- if you detect a redirect condition - destroy cached output, and then
- include the required PHP file for it to generate new output - or call a function which will produce the desired output in case you need something different
This approach will also be faster than a header-based.