Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster redirect, PHP or htaccess?

What is FASTER way to redirect a particular (specific) page, a PHP or htaccess? Considering that many different pages may need such redirect on a high traffic site. e.g. what is faster:

PHP (no database queries involved, just simple plain php redirect)

header("Location: /new.php",TRUE,301);

or

htaccess

redirect 301 old.php http://site.com/new.php
like image 788
Lana Popi Avatar asked Oct 04 '11 15:10

Lana Popi


2 Answers

Depends. In general, .htaccess will be faster because you won't have the overhead of invoking PHP. However -- if you've got 1000 redirects in a single .htaccess file at the document root level, then Apache will need to check every one of them on every page load. I.e., instead of just serving index.php, you're now going to have to do 1000 regex checks, and then serve index.php. Overall, I'd say that if you've got a lot of redirects and a lot of pages that won't be redirected, then do it in PHP. In this case, you don't pay any extra overhead for the pages that don't need to be redirected.

like image 125
Alex Howansky Avatar answered Sep 22 '22 14:09

Alex Howansky


.htacess are processed before php is called, so if you can create it, it will surely be faster

like image 30
Einacio Avatar answered Sep 22 '22 14:09

Einacio