Lana Popi
Lana Popi

Reputation: 101

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

Upvotes: 9

Views: 3175

Answers (3)

Watermark Studios
Watermark Studios

Reputation: 1183

If .htaccess is used at all, Apache will process it before any PHP processing begins. I haven't done the benchmarks on using one or the other, but when both are present, .htaccess will always be faster.

Also note that .htaccess redirect can be written using regex to apply to multiple old files, so less work on your end.

Upvotes: 3

Einacio
Einacio

Reputation: 3532

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

Upvotes: 7

Alex Howansky
Alex Howansky

Reputation: 53533

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.

Upvotes: 8

Related Questions