FeedBurner htaccess voodoo with WordPress 2

by oneafrikan

If you use FeedBurner to burn your feeds, then you probably want FeedBurner to retrieve the feeds from your site as normal, but you want _ALL_ your visitors to read your feed using FeedBurner.

The WordPress .htaccess solution between versions 1.5.x and 2.x has changed somewhat, so when I simply imported my old htaccess file after doing a full upgrade to the shiny new WP version, things didn’t work as planned… which is cool, since I’d been doing some work with htacess already, and felt I was ready to play with the apparent voodoo ;-)

So basically my logic went like this:

  1. Turn the rewrite engine on
  2. Set the rewrite base url to “/”
  3. Make sure that any request to http://oneafrikan.com is redirected to http://www.oneafrikan.com
  4. Do the same for the feed url
  5. Set the correct feed url for feedburner
  6. Set the correct feed url for everyone other than feedburner
  7. Set the correct comments feed url for feedburner
  8. Set the correct comments feed url for everyone other than feedburner
  9. Start processing for WordPress
  10. And then do some anti-spam stuff that isn’t really the point of this post

So now what happens is that whenever someone clicks on http://www.oneafrikan.com/feed/ or http://www.oneafrikan.com/comments/feed/, they get redirected to a feedburner feed that they can read and also subscribe to…

The flip side of this is that because I’m playing around with new themes for my blog and editing stuff, doing the redirection in htaccess is far easier for me than it is to go and manually edit files for each template.

The end result went quicker than I thought and although this is by no means the real .htaccess voodoo that is really possible with mod_rewrite, I did find it easier than I thought (but that may be because I’ve just done a project using .htaccess to create SE friendly URL’s) it would be.

So, in the spirit of sharing the love, here is the relevant code for you to cut and paste and do with as you like:


RewriteEngine On
RewriteBase /

### set all page requests to go to www.oneafrikan.com
RewriteCond %{HTTP_HOST} ^oneafrikan\.com$ [NC]
RewriteRule ^(.*)$ http://www.oneafrikan.com/$1 [R=301,L]
# same for feedburner
RewriteRule ^http://oneafrikan.com/feed?$ http://www.oneafrikan.com/feed/ [R]

##### feedburner
### redirect for feedburner feed
RewriteCond %{HTTP_USER_AGENT} ^FeedBurner.*$
RewriteRule ^feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?&feed=$1 [QSA,L]
RewriteCond %{HTTP_USER_AGENT} ^FeedBurner.*$
RewriteRule ^(feed|rdf|rss|rss2|atom)/?$ /index.php?&feed=$1 [QSA,L]

# redirect for users not subscribed
RewriteCond %{HTTP_USER_AGENT} !^FeedBurner.*$
RewriteRule ^(feed|rdf|rss|rss2|atom)/?$ http://feeds.feedburner.com/oneafrikan [R=permanent,L]
RewriteCond %{HTTP_USER_AGENT} !FeedBurner.*$
RewriteRule ^feed/(feed|rdf|rss|rss2|atom)/?$ http://feeds.feedburner.com/oneafrikan [R=permanent,L]

### redirect for feedburner comments feed
# http://feeds.feedburner.com/oneafrikancomments
RewriteCond %{HTTP_USER_AGENT} ^FeedBurner.*$
RewriteRule ^comments/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?feed=$1&withcomments=1 [QSA,L]
RewriteCond %{HTTP_USER_AGENT} ^FeedBurner.*$
RewriteRule ^comments/(feed|rdf|rss|rss2|atom)/?$ /index.php?feed=$1&withcomments=1 [QSA,L]

# redirect for users not subscribed
RewriteCond %{HTTP_USER_AGENT} !^FeedBurner.*$
RewriteRule ^comments/(feed|rdf|rss|rss2|atom)/?$ http://feeds.feedburner.com/oneafrikancomments [R=permanent,L]
RewriteCond %{HTTP_USER_AGENT} !FeedBurner.*$
RewriteRule ^comments/feed/(feed|rdf|rss|rss2|atom)/?$ http://feeds.feedburner.com/oneafrikancomments [R=permanent,L]
##### /feedburner

# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

Hope that helps someone ;-)
Does anyone have any better solutions or do things differently?