Redirect WordPress Feeds to Feedburner via htaccess (Redux)

Post #523 categorized as Function, WordPress, last updated on Dec 15, 2008
Tagged with feedburner, feeds, htaccess, plugins, redirect, WordPress

[ ~:{*}:~ ] In a previous article, I explain how to redirect your WordPress feeds to Feedburner. Redirecting your WordPress feeds to Feedburner enables you to take advantage of their many freely provided, highly useful tracking and statistical services. Although there are a few important things to consider before optimizing your feeds and switching to Feedburner, many WordPress users redirect their blog’s two main feeds — “main content” and “all comments” — using either a plugin or directly via htaccess. Here is the htaccess code as previously provided here at Perishable Press:

# temp redirect wordpress content feeds to feedburner
<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/perishablepress [R=302,NC,L]
</IfModule>

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

The first code block redirects main content feeds to Feedburner, while the second block does the same for the main comments feed. The only editing required for either of these redirects is for the Feedburner URL itself; that is, edit the “http://feeds.feedburner.com/perishablepress” and/or “http://feeds.feedburner.com/perishablepresscomments” to match your own address. With this htaccess code in place, your feeds will be redirected to Feedburner for everyone except FeedValidator and of course Feedburner itself. To add additional exceptions to the redirect, simply replicate either line containing RewriteCond, and edit the user agent (e.g., FeedBurner) to suit your needs. Of course, if you plan on using both of these redirects (i.e., all content and comment feeds), continue reading for a better technique..

New and Improved Method for Redirecting WordPress Feeds to Feedburner

For those of us using Feedburner for all content and comment feeds, we have consolidated the previous htaccess code into a single redirect. Additionally, we improve functionality by verifying the requested URI and simplifying the regex used to match the target string. Check it out:

# temp redirect all wordpress feeds to feedburner
<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REQUEST_URI}      ^/?(feed.*|comments.*)        [NC]
 RewriteCond %{HTTP_USER_AGENT} !^.*(FeedBurner|FeedValidator) [NC] 
 RewriteRule ^feed/?.*$          http://feeds.feedburner.com/perishablepress         [L,NC,R=302]
 RewriteRule ^comments/?.*$      http://feeds.feedburner.com/perishablepresscomments [L,NC,R=302]
</IfModule>

If you are redirecting both content and comment feeds, this “new and improved” htaccess code will do the job nicely. With optimized code and improved functionality, it is definitely recommended over the previous directives. Simply edit the URLs in the last two lines to match your own Feedburner addresses (one for posts and the other for comments), and place the code into your blog’s root htaccess file. No other changes should be required. Once the code is in place, test that everything is working by accessing your feed directly using its default (i.e., non-redirected) URL. After that, you can sit back, relax, and enjoy all the benefits of WordPress, Feedburner, and one less plugin to worry about.

<update> (12/15/2008) If either of your feeds will not redirect using the “new and improved” redirect code, try commenting out the following line:

RewriteCond %{REQUEST_URI} ^/?(feed.*|comments.*) [NC]

..and that should do the trick. Or, another option is to use the two “original” redirects provided in the beginning of this article. </update>

Subscribe to Perishable Press


49 Responses

TopLeave a comment

[ Gravatar Icon ]

#1Sumesh

Good post, and thanks for the htaccess code.

I’m a performance enthusiast myself, and couldn’t find any reason to keep FeedSmith when similar redirects are handled by htaccess. Only, I couldn’t get hold of the required code. Now I have :D

[ Gravatar Icon ]

#2Perishable

Thanks for the feedback, Sumesh! I am glad you found the code useful. It is amazing how many plugins are actually unnecessary for people who take the time to improve the performance of their site. One less plugin slowing things down is always a good thing! ;)

[ Gravatar Icon ]

#3Evan

Thank you, sheesh, you’d think that getting something like this set up was supposed to be a secret or something. Even the so called “how to’s” on the feed burner site are convoluted and difficult to follow… not to mention over 2 years old.

This was easy to follow, understand and install.

Thanks again!

[ Gravatar Icon ]

#4Perishable

I totally agree, Evan — trying to decipher the Feedburner documentation for this technique is like driving without a car, somehow terribly futile and frustrating. And that is precisely the reason for this article. I am glad you found it useful! Thanks for the feedback :)

[ Gravatar Icon ]

#5rukeba

Thanks! But this .htaccess may be applied to other non-wordpress feeds for redirecting to feedburner.

[ Gravatar Icon ]

#6Perishable

Yes, the redirect technique described in the article could very well be adapted to non-WordPress sites (i.e., WordPress is not required for them to work). So long as the rewrite directives target the correct URLs, this method should prove most effective. Thanks for the reminder!

[ Gravatar Icon ]

#7Sumesh from Blog Creativity

I have a couple of doubts -
1) Can this be used for WP 2.5? (FeedSmith doesn’t work for 2.5 anyway)

2) Should I place this code before or after the WordPress rules?

[ Gravatar Icon ]

#8Perishable

Hi Sumesh, I have not yet tested this method with WordPress 2.5, but I think it will work just fine. As far as I know, WP 2.5 still uses the same permalink structures as previous versions. The FeedSmith plugin uses PHP to handle the feed redirects, and thus may not work if WP 2.5 has changed the PHP mechanism by which canonical redirects are produced. Conversely, the htaccess method should not be affected by such changes as it operates independently of PHP scripts (generally speaking). In any case, the best way to find out is to upload a copy (place anywhere in root/blog htaccess) and give it a shot. Good luck! ;)

[ Gravatar Icon ]

#9Florian

Thanks for the snippet.
I had to place the code before the wordpress permalink rules. It didn’t work after the wordpress rules.

[ Gravatar Icon ]

#10Jeff Starr

Thanks for the tip, Florian! :)

[ Gravatar Icon ]

#11Ipstenu

Thank you! Feedburner’s been ticking me off, not updating right, for a while. I had to turn off Bad Behavior, fix the .htaccess, then change the feed, and THEN it worked. Sheesh. I’m tempted to go back.

FYI, I also added in |rss.* to the REQUEST_URI line, and duped the rewrite for ^feed as ^rss to get site.com/rss to redirect too. Wicked easy!

[ Gravatar Icon ]

#12Jeff Starr

Thanks Ipstenu, glad to hear you got it working.. also, matching and redirecting rss is keen — thanks for the tip!

[ Gravatar Icon ]

#13Marty Martin

I’d like to be able to redirect the main blog and comments to feedburner, but preserve the WP RSS feed for my tags. Right now though, if I try to visit the RSS feed for a specific tag, it redirects me to feedburner’s main content feed for my site. I’m not much of a REGEX person. How would I accomplish this?

A sample template RSS would be - http://www.foo.com/wp-rss2.php?tag=foobar

Thanks for any suggestion!

[ Gravatar Icon ]

#14Jeff Starr

@Marty Martin: Here is the code I use for this, placed directly before the WordPress permalink directives:

# REDIRECT MAIN AND COMMENT FEEDS TO FEEDBURNER
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !^.*(FeedBurner|FeedValidator) [NC]
RewriteRule ^feed/?$ http://feeds.feedburner.com/yourmainfeed [L,NC,R=302]
RewriteRule ^comments/feed/?$ http://feeds.feedburner.com/yourcommentfeed [L,NC,R=302]
</IfModule>

Replace the yourmainfeed and yourcommentfeed with your main feed and comment feed, respectively. Upload to your blog’s root htaccess file and you should be good to go!

[ Gravatar Icon ]

#15Alexander

Great tutorial, however I would like to redirect all my category feeds through feedburner, say I already had the feedburner feed created for each category, would inserting redirects for all my categories on the .htacces slow down my page loading?

I asume the process for the categories RSS is the same as for the content and comments redirection, say I have a “Google” category, I assume wildcards are usable here (newbie modifiyng an .htacces ^^)

The google feed is:
http://www.tecnovits.com/category/google/feed

So on the REQUEST_URI I could modifiy it like this so it would permit all my categories:
RewriteCond %{REQUEST_URI} ^/?(feed.*|comments.*|category/*/feed.*) [NC]

And the redirect would be something like:
RewriteRule ^category/google/feed/?.*$ http://feeds.feedburner.com/tecnovits-google [L,NC,R=302]

So the whole thing would be like:

# temp redirect all wordpress feeds to feedburner

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/?(feed.*|comments.*|category/*/feed.*) [NC]
RewriteCond %{HTTP_USER_AGENT} !^.*(FeedBurner|FeedValidator) [NC]
RewriteRule ^feed/?.*$ http://feeds.feedburner.com/perishablepress [L,NC,R=302]
RewriteRule ^comments/?.*$ http://feeds.feedburner.com/perishablepresscomments [L,NC,R=302]
RewriteRule ^category/google/feed/?.*$ http://feeds.feedburner.com/tecnovits-google [L,NC,R=302]

Would be really helpful if anyone could check this for me, before I modify the .htacces
Thank you.

[ Gravatar Icon ]

#16Jeff Starr

@Alexander: Firstly, depending on the number of categories you have, implementing htaccess redirects for each of them should result in no noticeable difference in page-loading times. Each directive included in your htaccess file(s) does require an extra bit of processing, but unless you have an large number of categories (or a slow server), performance should not be affected by much.

Secondly, when it comes to htaccess, a wildcard operator is specified by a literal dot ( . ), which will match any character in the target string. Further, to match any number of any type of character, an asterisk ( * ) is placed after the dot, like this: ( .* ).

Lastly, the example RewriteCond that you specify will match against category feeds even without the extra expression, “category/*/feed.*”, due to the presence of the first “feed.*” statement. That is, any string matched by the second expression will also be matched by the first.

Other than that, it looks like your code will work just fine, although I haven’t tested it for myself, so I am hesitant to say for sure. Just remember to change the feed URLs to yours instead of mine! ;)

[ Gravatar Icon ]

#17Alexander

Thanks for the heads up!
I actually tested it and it worked fine, I also followed your advice and removed the condition and everything well, also no noticeable slow down.

Thank you.

PD: Although in an ironic twist of fate I stopped using feedburner in the morning due to bad service by them. :P

[ Gravatar Icon ]

#18Michael

Hello! This is a great tip for redirecting feeds… I am hoping there is a way to use the rewrite when using “Domain Mirror” — having two domains on the same WP install.

Is there some way to change ^feed/?.*$

To be like ^firstdomain.com/feed/?.*$
and ^secondomain.com/feed/?.*$

Is it as simple as that? Or does the ^ character mean something else?

I’d appreciate any insight on this, thanks.

- Michael

[ Gravatar Icon ]

#19Jeff Starr

@Michael: The carat symbol ( ^ ) is used to denote the beginning of a string. It essentially says, “begin your pattern match at this point.” I am not familiar with “Domain Mirror,” but I understand the concept. Assuming both domains are located on the same server, the modified redirect code may indeed be as simple as you suggest, although I have not tested myself. Use caution when testing, of course. ;)

[ Gravatar Icon ]

#20Brian St. Pierre

Thanks for posting this. It got me 90% of where I wanted to be…

The problem I needed to solve after migrating from Blogger to WordPress was redirecting subscribers from http://blog.bstpierre.org/feeds/posts/default (the old Blogger feed) to http://blog.bstpierre.org/?feed=atom.

So I was adding a rule like:

RewriteRule ^feeds/posts/default$ http://blog.bstpierre.org/?feed=atom [L,NC,R=301]

But the rules above were firing instead, because “?” is a special character that means “match 0 or 1 of the preceding” thus “feed/?.*” matches “feeds…” and steals my redirect.

I think what you really want is:

RewriteRule ^feed/[?].*$ http://feeds.feedburner.com/perishablepress [L,NC,R=302]

[ Gravatar Icon ]

#21Jeff Starr

@Brian St. Pierre: Yes, this technique is designed to redirect WordPress-formatted feeds to their Feedburner counterparts. Adjustments may be required to account for the subtle differences in syntax for alternate feed URLs (e.g., Blogger feeds). For WordPress, however, the method is sound, and used successfully on many sites.

As for the literal question mark, it is not used in this technique as we are targeting permalink-formatted WordPress feeds. The optional match character (“?”) is used primarily to account for the presence (or absence) of a trailing slash on the feed URL(s).

For redirecting non-permalink WordPress feed URLs, we target the various query strings themselves rather than using a literal question mark (as described in your comment). For more information on this, check out this article.

[ Gravatar Icon ]

#22Patrix

I’m using FeedBurner and the Feedsmith plugin for my filter blog, DesiPundit. I found your post via the WordPress page for RSS feeds and this definitely looks much better.

But I’ve a question. I have three sections on my blog; Community where I present (in excerpt form) syndicated feeds from other veteran bloggers, New & Upcoming where I link to posts from bloggers not in the first section with a short personal description of the link, and finally Featured which is kinda the best of the first two sections. Now my problem is that I want to offer full feeds but if I do, the posts in the first section show full feeds of the bloggers that I syndicate which may not be right as I want readers to click through to their blogs. But I don’t mind showing full feeds for the second section which is a short description of the link anyway and readers do click thru.

So my question is, how do I offer partial feeds for one main category and its sub-categories while offering full feeds for the other category? The sections are defined by three parent categories. I would really appreciate if anyone has a solution.

[ Gravatar Icon ]

#23Simon

I was trying to get this to work for a few hours, didn’t quite redirect in the end. How can I just redirect the main feed to (domain/feed) to the feedburner feed? So I can still use all the other feeds for individual purposes?
Cheers and have a great evening

[ Gravatar Icon ]

#24Jeff Starr

@Patrix: Unfortunately, we can’t use HTAccess to control whether or not a feed is full or partial, however, I do recall seeing a plugin called “Feed Control” (or something similar) that provides granular control over the formatting of your various feeds. If you can’t find a plugin or function to do the job, you may want to try experimenting with Yahoo! Pipes, which is a feed aggregator that enables the creation of custom feeds, formatting, and so on.

@Simon: Great to see you again! The technique described in the article should do exactly what you need. Make sure you read the update note at the end of the post if you are experiencing any issues.

[ Gravatar Icon ]

#25Simon

Hi Jeff, same here! It seems your blog always holds the answers to some of my questions :)
Thanks for the fast response. I think I tried everything but it never redirects anything. I just put it in the httaccess right? Maybe below # END WordPress or fo i put it in there?
Cheers Simon

[ Gravatar Icon ]

#26Jeff Starr

@Simon: Before the WordPress permalink rules! And, be sure that you try this method as well:

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

If that doesn’t work, there may be something else that is interfering..

[ Gravatar Icon ]

#27Simon

Juhui (swiss exclamation of joy)! Works like a charm. One less plugin in my neverending list. You’re a genious the last one workes perfectly. :)

[ Gravatar Icon ]

#28Jeff Starr

Excellent! Happy to help, Simon! :)

[ Gravatar Icon ]

#29Justin Tadlock

Jeff, I hope you don’t mind me troubling you for a moment, but this is far from my area of expertise. Your first set of code above works great for me. It always has.

I have one feed in particular that I need to exclude from being redirected (http://justintadlock.com/feed/wordpress-o-sphere), which is a custom-created feed. I don’t need it redirected at all, but I’d like to redirect everything else.

The rewrite rule currently is:

RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds2.feedburner.com/JustinTadlock [R=302,NC,L]

I usually figure these things out pretty quickly, but I’m stuck on this one. If you have a moment and know how to do this, I’d appreciate it if you’d lend a hand. I’ve got a tutorial coming up on creating custom feeds, but I can’t show off the feed because it’s being redirected to Feedburner.

[ Gravatar Icon ]

#30Jeff Starr

Sorry for the delay on this - had my wisdom teeth out and things got quite hectic..

That is tricky one for sure, but a few things come to mind that might be of some help..

1) If at possible, use a different directory for the custom feed. Your current rewrite rule would then skip the custom-feed directory, enabling you to simply create a new rule for it.

2) Depending on how many feeds you have at your site, exchange the wild-card pattern-matching for a specifically targeted list of feeds (e.g., any feed that actually goes to Feedburner). As with the first option, this would enable you to target as many custom feeds as needed.

3) In general, rewrite directives for excluding requests for specific patterns that are included subsequent conditions is difficult. You may have luck using mod_rewrite for the general pattern-matching directives and then mod_alias for the exclusive case (i.e., for the custom feed).

I hope that proves useful - let me know if I may be of any further assistance.

[ Gravatar Icon ]

#31Justin Tadlock

Thanks. I don’t know why I just didn’t think to change the name of the custom feed directory. That makes things super easy. Sometimes, it just takes a fresh set of eyes to find the simplest things.

[ Gravatar Icon ]

#32Jeff Starr

Anytime, Justin! Your articles have helped me on many occasions — more than happy to return the favor :)

Share your thoughts..

TopRead official comment policy

The rules are simple. Comment intelligently. Stay on-topic. Don’t spam! Suspected spam will be deleted. Use your real name or nickname, not a site name or business name. Using a site name or business name is a good way to get your link or comment removed. Certain comments are moderated; if your comment does not appear after several days, or if you wish to comment privately, contact me. Also, by posting a comment, you grant this site a perpetual license to reproduce your comment, name, and website URL. Lastly, you may use basic HTML markup, but please do not use <pre> tags. Instead, wrap your code with <code> tags. Use a new set of <code> tags for each code term or phrase, as well as for each individual line of code (i.e., multiple lines of code require multiple code tags). Please see the complete comment policy for more information.