The Blueprint
Expression Engine - Redirecting your feed to Feeburner
April 14, 2008 by Ryan in Code, Expression Engine
Our blog has been up and running for a while and we’re seeing some traffic, but Expression Engine just doesn’t have the stats control to really be able to see how many people are subscribing to our feed or what their stats might be. Feedburner seemed like the most logical choice for us to migrate our feed to because there are such a wide variety of stats that they track. The biggest question was, “How do we do it seamlessly so that our current subscribers aren’t affected?” Having to remove the old feed and then add the new one just because we want to change something is just plain bad form.
I’ve seen some plugins for Wordpress that hook right into the code and redirect it effortlessly, so I figured maybe someone had written one for Expression Engine. After doing a little research through the EE forums and repository, I came up with nothing of any real use. My thoughts turned to handling the request on the server side of things instead of the actual EE framework. I could do this pretty easy by adding in an .htaccess file with some URL ReWrite code. I’d seen this done for Textpattern by my friend Nathan Smith, so I knew “in theory” it would work just fine. Knowing the devil is in the details, I brushed off my regular expression skills and wrote a piece of code to make our EE feed redirect to Feedburner.
The first thing to do is create an .htaccess file, assuming you don’t have one, which will reside in your main EE directory. Place the following code inside that file being sure to change the URL to your Feedburner feed.
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect all feeds requests to Feedburner
#------------------------------------------------------------------
RewriteCond %{REQUEST_URI} ^.*(rss_2\.0) [NC]
RewriteCond %{HTTP_USER_AGENT} !^.*(FeedBurner|FeedValidator|Recent) [NC]
RewriteRule ^(.*)$ http://feeds.feedburner.com/intereactive [L,R=302]
#------------------------------------------------------------------
</IfModule>
Our RSS feed template in Expression Engine is named rss_2.0. Previously, you would subscribe to it by going to the URL http://intereactive.net/blog/rss_2.0, so what we needed to do was redirect that URL to Feedburner behind the scenes. If we take a deeper look into the code we’ll see exactly how I did that.
RewriteCond %{REQUEST_URI} ^.*(rss_2\.0) [NC]
We use {REQUEST_URI} to grab the URL and then search for rss_2.0 (our template name) by using the RegExp code ^.*(rss_2\.0)
RewriteCond %{HTTP_USER_AGENT} !^.*(FeedBurner|FeedValidator|Recent) [NC]
We use {HTTP_USER_AGENT} to look at where the request is coming from and by using the ! (which means NOT) we can see if Feedburner is requesting the original feed. The reason we want to know this is that if we don’t cancel the redirect for Feedburner, it could get caught in an infinite loop.
RewriteRule ^(.*)$ http://feeds.feedburner.com/intereactive [L,R=302]
This part of the code actually does the redirect. It’s pretty straight forward but I wanted to note that this code only runs if all the previous RewriteCond statements return as TRUE. Also note that you will need to put your Feedburner URL in the code and not ours.
Hopefully that helps some of you migrate your existing EE to a service such as Feedburner. Here are some references that may help you with RegExp’s and RSS feeds for EE:
Photo’s not showing up or text unformated in your EE feed?
The basics of RegExp’s in regards to ModRewrite
Some examples of ModRewrite