WordPress Feedburner HTAccess Redirect for Default (Non-Permalink) Feed URLs

[ ~{*}~ ] Recently, a client wanted to deliver her blog feed through Feedburner to take advantage of its excellent statistical features. Establishing a Feedburner-delivered feed requires three things: a valid feed URL, a Feedburner account, and a redirect method. For permalink-enabled WordPress feed URLs, configuring the required redirect is straightforward: either install the Feedburner Feedsmith plugin or use an optimized HTAccess technique. Unfortunately, for sites without permalinks enabled, the Feedsmith plugin is effectively useless, and virtually all of the HTAccess methods currently available on the Web are designed for permalink-enabled configurations. In this article, we will modify our existing HTAccess technique to work with default WordPress feed URLs.

Default WordPress Feed Format

By default, WordPress feed URLs look similar to the following:

Main Feed:
http://domain.tld/blog/?feed=rss
http://domain.tld/blog/?feed=rss2
http://domain.tld/blog/?feed=atom

Comment Feed:
http://domain.tld/blog/?feed=comments-rss2

..depending on your preferred WordPress feed URL. These default (non-permalink) feed URLs employ query-string parameters to specify the type and format of each feed. Unfortunately, these dynamic URL formats do not work with Feedburner’s own Feedsmith redirect plugin. Fortunately, we can skip using a plugin altogether and use a little HTAccess magic instead.

HTAccess Redirect for Permalink-Enabled WordPress Feeds

Let’s review the code required to redirect all permalink-formatted WordPress feeds:

# TEMP WORDPRESS FEEDBURNER REDIRECT for PERMALINKS
<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>

..which works great for redirecting your permalink feeds to your Feedburner feed. To accomplish this, the RewriteCond matches the “feed” or “comments” pattern in the requested URI. The RewriteRule then redirects all matched URLs to the established Feedburner feed. So, to get this HTAccess method to work with default (i.e., dynamic) WordPress feed formats, we need to modify the RewriteCond to match against the QUERY_STRING rather than the REQUEST_URI.

HTAccess Redirect for Default WordPress Feeds

For WordPress sites without permalinks enabled, we must modify our rewrite rules to recognize feed parameters located in the query string of requested URLs. The resulting HTAccess code for redirecting all default (non-permalink/dynamic) WordPress feeds to Feedburner looks like this:

# PERISHABLE PRESS FEEDBURNER REDIRECT for DEFAULT WORDPRESS FEEDS
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{HTTP_USER_AGENT} !^.*(feedburner|feedvalidator)                        [NC]
 RewriteCond %{QUERY_STRING}     feed=(rss|rss2|atom).*                               [NC]
 RewriteRule ^(.*)$              http://feeds.feedburner.com/ContentFeed?             [NC,R=302]
 RewriteCond %{QUERY_STRING}     feed=(comments\-rss|comments\-rss2|comments\-atom).* [NC]
 RewriteRule ^(.*)$              http://feeds.feedburner.com/CommentFeed?             [L,NC,R=302]
</IfModule>

When placed in your site’s root HTAccess file (or Apache config file), this code will redirect all requests for your default WordPress feeds to your specified Feedburner feed. Simply edit the “ContentFeed” and “CommentFeed” portion of the RewriteRule to match the corresponding Feedburner URLs. No other editing is required. This code will match and redirect all requests (unless from FeedBurner or FeedValidator) for both your main content and comment feeds in any format. As expected, category, archive, and individual comment feeds are not affected by this code. This method is more comprehensive than the Feedsmith plugin, but not so aggresive as to redirect your peripheral feeds (i.e., any feeds other than your main content and main comment feeds).

Breakdown

Before applying new code, it’s always smart to understand its functionality, especially when it comes to HTAccess. Fortunately, this redirect method is fairly straightforward, matching various regex patterns and redirecting them to their new location. As a precautionary measure, the entire method is enclosed within an IfModule container to check that the required Apache module (in this case, mod_rewrite) is available before executing the enclosed directives.

Within this container, the first step is to enable the RewriteEngine. If the RewriteEngine has already been enabled, this directive may be omitted. The remaining four lines of this method accomplish the matching of the desired feed URLs and their subsequent redirection to the specified Feedburner feeds. To do this, first RewriteCond directive checks that the user agent is neither feedburner nor feedvalidator (in case-insensitive [NC] fashion). The two subsequent RewriteCond directives then match any instances of the default feed parameters (e.g., atom, rss, rss2) in the QUERY_STRING of the requested URL.

Finally, any URLs that are matched by the RewriteConditions are rewritten to their specified Feedburner URL. As this temporary redirect occurs, the query string is removed from the rewritten URL via the question mark appearing near the end of each RewriteRule.

Of course, that is merely an overview of the process; there is of course much more happening both logically and functionally, but this article is getting long enough as it is, and I certainly wouldn’t want to bore you with all of the in-depth technical details. Perhaps I will dig into the deeper mechanics of such methods in a future article..

Redirect Only the Main Feed

For the record, here is the code required to redirect only the main RSS2 feed to Feedburner:

# PERISHABLE PRESS FEEDBURNER REDIRECT for the DEFAULT WORDPRESS RSS2 FEED
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{QUERY_STRING}     feed=rss2                                [NC]
 RewriteCond %{HTTP_USER_AGENT} !^.*(feedburner|feedvalidator)            [NC] 
 RewriteRule ^(.*)$              http://feeds.feedburner.com/ContentFeed? [L,NC,R=302]
</IfModule>

As before, the only edit that needs to be made is to change the “ContentFeed” portion of the RewriteRule to match the URL of the target Feedburner feed. Once in place, this method will redirect all requests (except for those from Feedburner and FeedValidator) for your main feed to the assigned Feedburner URL.

Later Daze

If you need to redirect the default, non-permalink versions of your WordPress feed URLs to their associated Feedburner accounts, the HTAccess techniques presented in this article will do the job nicely. 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.