Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to determine if using Apache's .htaccess mod rewrite is a faster way to redirect to a PHP file, compared to redirecting from one PHP file to another, using the header redirect core function of PHP.

I looked around and I was unable to find anything about this subject. Any help would be appreciated.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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:

  1. 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
  2. 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:

  1. cache all the output you generate in your PHP app
  2. if you detect a redirect condition - destroy cached output, and then
  3. 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.

share|improve this answer
    
Very interesting Chronos! +1. on my shared hosting no virtual host is permitted. before i use apache redirect, which i will do, i want to first try you advise for php method: i have now this in my index.php switch($_SERVER["HTTP_HOST"]){ case "website.com": header('HTTP/1.1 301 Moved Permanently'); header('Location: /english/home'); case ... etc ?> How to i cache all the output,and then destroy the cached output? which code does that? Thanks! –  Sam Dec 18 '10 at 6:36

My gut tells me that htaccess would have to be faster, but there's no substitute for actually benchmarking it in your particular environment.

share|improve this answer

One thing to keep in mind is that using .htaccess files slows everything down (if the setting is even on). Putting your rewrite in a in one of your .conf files for Apache (httpd.conf or virtual-hosts.conf) and then setting AllowOverride none will be even faster.

As for php vs mod_rewrite, it's hard to say. They are both so fast I doubt it matters.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.