up vote 0 down vote favorite
share [g+] share [fb]

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.

link|improve this question
feedback

4 Answers

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.

link|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
feedback

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

link|improve this answer
feedback

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.

link|improve this answer
feedback

Regular expressions are ALWAYS slower than any script performing the same task. Especially when the script's bytecode is cached :)

link|improve this answer
1  
I was hoping that waiting for mod-rewrite to initialize and redirect would be faster than waiting for PHP to initialize and perform the redirect. The way I imagine how it would work is the Apache instance is already running, and the request hits Apache before anything else, so even if both instances of Apache and PHP were running, it might be faster to run the request from the very first engine the request encounters. I know using a Regular Expression does probably slow the process down quite a bit, but it does seem to be running quicker.. I need to find a good way to benchmark it. – r.crandall Nov 27 '09 at 23:00
1  
regexp has to do a lot of processing, but I can't imagine it would be slower wallclock EVER compared to having client make multiple HTTP requests over a network. – Xepoch Nov 27 '09 at 23:54
feedback

Your Answer

 
or
required, but never shown

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