Redirecting Wordpress feeds to FeedBurner with PHP

I just finished to read the following mini tutorial from WPBeginner:

Recently we featured an article that showed a step by step guide to setup FeedBurner in which we shared the plugins you can use to redirect. In this article, we will show you how you can redirect WordPress RSS Feeds to Feedburner without using a plugin (This tip is for intermediate to advanced users).

First open your .htaccess file which is located in your root directory (Hint: same folder where wp-config.php is located). Then paste the following code:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-] )?/?$ http://feeds.feedburner.com/wpbeginner [R=302,NC,L]
</IfModule>

By the way, don’t forget to change the feed URL to your own Feed URL otherwise you will be redirecting users to our Feeds.

I don't think this is a good idea and for one simple reason: performance. .htaccess files are processed by Apache every time that a certain directory is requested by a client. The more declarations you have inside an .htaccess file, the more the server will spend its resources to serve a given request. This is something that you can do by simply using PHP. What we've got so far? We have:

  1. a directory where our feeds are located (in this case /feed)
  2. a FeedBurner URL where to redirect our requests.

You have to create a simple PHP function to be used when an user requests our feed directory:

function redirectFeed()

{

$feed_request = $_SERVER['HTTP_REQUEST'];
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);

if(!preg_match('/feedburner|feedvalidator/', $ua) {

    if(preg_match('/feed/.+/', $feed_request)) {
    
        header('Location: http://feeds.feedburner.com/yourfeed');
    
    }

}

}

Simply insert this function in an empty Wordpress page template when you need to perform such a redirect.

Note

Please test the above code before using it. I didn't test it myself simply because I don't use Wordpress on this blog.

Leave a Reply

Note: Only a member of this blog may post a comment.