Redirecting Wordpress Feeds to Feedburner With mod_rewrite
A lot of WordPress bloggers use FeedBurner for their RSS feeds thanks to the wealth of features available. So all you need to do is redirect any feed request to your WordPress blog to your FeedBurner blog. Should be pretty easy, but you’d be surprised. In being as versatile as it is, WordPress can return all sorts of RSS feeds based on how it’s asked. Add in a separate comments feed on FeedBurner and things get a little tricky. It’s easy to redirect the most common feed URLs directly to FeedBurner using simple mod_rewrite rules or the FeedSmith plugin that FeedBurner now maintains, but they are not perfect solutions.
The problem is that most plugins redirect only the most common feed URLs while others still seem to come through to WordPress. I noticed this on a few of my sites where most feed traffic hit FeedBurner, but some non-FeedBurner feed requests were still being served. mp3 at AskApache saw the same thing and came up with some nifty mod_rewrite rules to handle sending the proper requests to FeedBurner. However his rules required your comment feed to be formatted a certain way. It also seemed like certain very obscure feed requests might get improperly handled.
But I still wanted to move my redirects to mod_rewrite. Feed requests are one of the ‘heavier’ WordPress requests out there in terms of CPU processing. Using a plugin to redirect your feeds helps, but you also end up initializing WordPress and PHP to do a simple redirect. Feed requests are happening more and more as legit and not so legit bots look for new content - so the less load they have the better. Using mod_rewrite is much more efficient since WordPress never gets initialized. It’s all handled within Apache.
So I sat down and came up with some rewrite rules, based on mp3’s rules, to try and better accomodate the various WordPress feed requests, common and obscure, without requiring a special type of FeedBurner feed name for my comment feed. I was previously using the Permalink Redirect plugin to handle redirects for my FeedBurner feed AND my hostname. Since I was doing this, I figured I’d try to get rid of Permalink Redirect completely and do it all within .htaccess. One less plugin to run meant that much less initialization for WordPress on normal requests. Here is what I came up with. I included comments to show the logic behind some of the rules
# Force use of specific hostname
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^/(.*) http://www.example.com/$1 [L,R=301]
#
# Redirect global post and comment feeds to Feedburner without loading WP
RewriteCond %{REQUEST_URI} ^/(feed|wp-atom|wp-feed|wp-rss|wp-rdf)(.*) [NC,OR]
# Only match if feed is the ONLY attribute. Any other attributes mean a custom feed
RewriteCond %{QUERY_STRING} ^feed=(feed|rss|rss2|rdf|atom)$ [NC]
RewriteCond %{HTTP_USER_AGENT} !^.*(FeedBurner|FeedValidator) [NC]
RewriteRule .* http://feeds.feedburner.com/MyPostFeed? [L,R=302]
#
# Comment feeds can be called via /comments, wp-commentsrss2, or withcomments=1 to the main feed script
RewriteCond %{REQUEST_URI} ^/(comments/|wp-commentsrss2)(.*) [NC,OR]
RewriteCond %{QUERY_STRING} ^.*withcomments=.*$ [NC]
# Calls directly to the feed scripts that include ‘withcomments’ limit to POSTS with comments
RewriteCond %{REQUEST_URI} !^/(wp-atom|wp-feed|wp-rss|wp-rdf)(.*) [NC]
# Any specification of a post ID we skip since it’s post specific
RewriteCond %{QUERY_STRING} !.*p=.* [NC]
RewriteCond %{HTTP_USER_AGENT} !^.*(FeedBurner|FeedValidator) [NC]
RewriteRule .* http://feeds.feedburner.com/MyCommentFeed? [L,R=302]
If you don’t have a comment feed on FeedBurner, just drop the 3rd section. Replace ‘MyPostFeed’ with the name of your FeedBurner post feed and MyCommentFeed with your FeedBurner comment feed.
So far this has worked well for even the most obscure of feeds, like the dual behaviors of the attribute withcomments (when set in a main feed request, you get the comment fed - when set in a specific feed request, you get posts with comments). These were tested with WordPress 2.3.x There may be other weird cases from older WordPress versions it doesn’t cover. But so far it’s working like I expected. If you have ideas to enahnce it or find cases where the rules don’t work like they should, let me know in a comment.