0

I would like to know if there is any difference, in terms of efficiency (i.e. less use of server resources) in performing 301 redirects using .htaccess or using the wp_safe_redirect feature of Wordpress? (which would be implemented via a plugin).

I don't mind putting the redirects into the .htaccess file by hand. But, if there is no greater overhead having a plug-in do the redirects (using wp_safe_redirect) then I'd rather go that route for the sake of simplicity (for other site admins).

Can anyone suggest which of these methods would have the least impact on the server?

After-thoughts / Answer

Considering the points made by both CYBMETA and Mark Kaplun, I get the impression the answer to my question is not a black and white one. I now understand it depends on many factors, and in most usage scenarios may make very little practical difference whether I use .htaccess or a plug-in.

A plug-in provides ease-of-use, but will have some degree of added overhead for the PHP to execute, and for the Wordpress core to load twice in order to deal with the redirect, and then deal with rendering the page. I realise that on an adequately configured server, this is unlikely to create a significant performance hit, unless the site is very busy and the redirected URIs account for a significant number of the site hits. I understand using .htaccess for the redirections means additional lines in the .htaccess have to be parsed for every resource request to the site, whether it's for the redirect URIs or not. Again, on an adequate server, that should have minimal real world impact. Using a plug-in is certainly more convenient for the end user, if numerous redirects need to be set up over time.

So, with these understandings in mind, I am not able to check either answer as the "accepted answer". I accept them both on their own merits, and have marked them up accordingly.

2

.htaccess allow you to perform the redirection without the need of any single line of PHP. Using WordPress to do redirections means that WordPress engine is loaded, then the user is redirected and WordPress is loaded again in the new location. So, as general rule, .htaccess redirections requires less servers resources. If you don't need interaction with WordPress to know when and where to do the redirection, use .htaccess.

  • Great. So, based on this, looks like I'll stick with using .htaccess. Any comment on whether the server impact from Wordpress handling the redirects is likely to be of any significance? (I am mostly now thinking of other sites, owned by clients, where editing .htaccess is something they would not be comfortable with). – omega33 Mar 31 '16 at 9:43
  • Sorry, I have no benchmark. Anyway, who are going to manage the redirection needs to be comfortable doing the job on the top, in my opinion. I though you asked for yourself. – cybmeta Mar 31 '16 at 13:27
1

As with all performance issues the answer is probably "it depends"

And here it will depend upon what is the ratio of "normal" traffic to the one requiring redirect.

The thing with doing it in htaccess is that on every request the file needs to be read and parsed and some url matching needs to be done. With naive implementation of 301 in htaccess, of the kind of "oh just put some line anywhere you want in the file", it means that time will be wasted even when serving images.

On the PHP side it depends how good is your redirect implementation, and probably they are all not optimal. If your server is running PHP7 with apcu based object caching in a way that do not require any DB access to detect and handle a redirect, the implementation might be very fast.

Lets assume now that the php solution is 10k times slower then the htacces. According to some stats - http://www.sitepoint.com/average-page-weight-increases-15-2014/ an average page does about 100 http request, serving each of them accures the extra "penalty" of the htaccess change. Going with those numbers, if your redirect is less then 1% of pages being served, the php solution will have better performance.

To conclude, for reasons of stability and ease of development and maintenance, you should avoid htaccess changes whenever possible. To go with htaccess solution you need to prove that it is much better, something that is not easy to do, and you might waste more effort on doing it then the effort that is required to serve redirects in a less optimal way, and then of course all you have achieved is having your CPU utilization on average of 49% instead of 50%.

Micro optimization like that are important only for facebook and google, the rest of the sites would have wished to have enough traffic for such an issue to be a noticeable problem.

  • Thanks for this detailed explanation. As there are only a few pages needing redirection (due to sub-optimal permalinks going through an overhaul) there will be relatively few redirection requests. AND, as there may be other users wanting to set up the occasional redirect... I have now decided to go with a Wordpress plug-in approach. Am I correct in thinking that a plugin using wp_safe_redirect is basically the right approach? – omega33 Apr 2 '16 at 3:35
  • Performance wise it doesn't matter much, the fact that all configuration is at one place and you don't have to hunt it down if anything goes wrong or bring the site down to make a change is the main thing. – Mark Kaplun Apr 2 '16 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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