Separate Feedburner feeds and categories feeds using .htaccess
20/11/2007It started out as a request to Patrix to give me a feed that was separate from his one liner asides. The problem was that every time I added his feed to my reader, his one liner posts took over most of the items in my reader. And since I don’t use feed readers [Why? Its a topic of another post], and prefer to use a feed widget on my block, more often than not I would find that there were more one liners than posts by others. However as mails with Patrix revealed, separating the feeds is not a simple thing to if you use feedburner as your primary feed.
That led me to delve into some research about how to accomplish this. I wanted the main feed to be unharmed by feedburner, and yet find a way to provide a separate feed based on what my readers could ask for. A couple of options were available – Permalink redirector plugin nearly did what I wanted it to do, and Feedburner category just about fell short.
Enter mod_rewrite. I stumbled upon this as I was doing a bit of research, and hit upon some excellent posts here and here. [Please do read those posts before going ahead as they are excellent source of information] I had done some work on rewrites a while back in my last job, so it wasn’t difficult to figure out what was done. And with a few little tweaks, voila, i don’t need any plugin (Feedburner or Permalink) to redirect my feed to feedburner. At the same time, I can give out any selective feed based on categories to my readers.
How did I do that? Simple – just by adding the following line of codes at the very top of the .htaccess file. The .htaccess file resides in the root directory of your wordpress install. For example, if your wordpress install was located at www.mydomain.com/wp/ then your .htaccess would reside in wp directory.
Here’s what I put at the top of my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !FeedBurner
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/stupendousman [R,L]
RewriteRule ^feed=rss2&cat=-269/?$ http://www.suyogdeshpande.net/blog/?feed=rss2&cat=-269 [R,L]
What does this say?
Simple – the third and fourth line simply say that if the user agent accessing your site is not feedburner, and if it is looking for a feed, then simply redirect it to feedburner. For example, if i type in http://www.suyogdeshpande.net/blog/feed/ then i will be redirected to feedburner feed for me. To make it work on your site, simply substitute my website for yours
The next line is more interesting.
RewriteRule ^feed=rss2&cat=-269/?$ http://www.suyogdeshpande.net/blog/?feed=rss2&cat=-269 [R,L]
I define another rewrite condition which simply says, if anybody comes looking for a feed without my slugs category (which is id=269), then instead of redirecting to feedburner, keep the original feed. By having a negative sign in “&cat=-269”, I assure that all categories except Slugs is added as a part of the feed.
Thats it! By using the simple rewrites I can keep adding or modifying the type of feeds based on what my readers request. For example, If somebody wanted my reviews feed, then I would simply add another line like:
RewriteRule ^feed=rss2&cat=29/?$ http://www.suyogdeshpande.net/blog/?feed=rss2&cat=29 [R,L]
Where cat=29 indicates my Reviews feed.
There are some caveats with the approach though. If you have a post in more than one category, and you chose to exclude the category, then the post in that category is excluded, even if it was a part of another category. I have not found a work around for this – if you find any, let me know. At some point I want to mix and match feeds from different categories. I am pretty sure it can be done, I just haven’t invested that time in it. When i do, I will let you know
I wish somebody expanded on this idea and made it into a wordpress plugin – it would be the most useful!
And please don’t forget to keep a backup of your .htaccess file!!
There are 6 comments in this article: