A few fixes for your RSS feed

Thanks to faithful readers Louis and Daniel, I fixed my feed, which wasn’t recognized by certain RSS aggregators. I also apologize for inconvenience to all of you who had such problems. My FeedDemon will digest anything, so I had no clue something’s wrong. Anyway, shame on me.

Incorrect header syntax

The problem was in incorrectly labeled header Last Modified, which is correctly spelled Last-Modified (with dash). In case you missed that lesson, a HTTP header field name can not contain spaces. For example, the Content-Type is correct, the Content Type is incorrect. Also from the HTTP specs:

“Any header fields which are not understood should be ignored.”

CDATA–what it’s good for?

Some blog management systems output feeds with a post content within legible opening <content:encoded> and closing </content:encoded> tags. This way the original HTML structure (with its’ elements like headings or paragraphs) can be preserved and viewed in your favorite RSS reader. Since XML syntax is very strict, one must also escape those tags by placing <![CDATA[ before and ]]> after the content, so XML processor knows those tags are not part of the XML structure.

“CDATA sections MAY occur anywhere character data may occur; they are used to escape blocks of text containing characters which would otherwise be recognized as markup. CDATA sections begin with the string <![CDATA[ and end with the string ]]>

Absolute vs. relative paths

If you run your feed through Feed Validator, it might throw you an error regarding relative paths to your images or files. This can be fixed by simple preg_replace() snippet, for example:

    echo '<content:encoded><![CDATA[';
    echo preg_replace('/<(img src|a href)="\//si', '<$1="http://' . $_SERVER['HTTP_HOST'] . '/', $content);
    echo ']]></content:encoded>';

The above ensures that every <img src="/image.jpg" /> is replaced with <img src="http://yoursite.com/image.jpg" /> and also <a href="/file.zip" /> is replaced with <a href="http://yoursite.com/file.zip" />. If you use different paths, for example ../../image.jpg, the above will not work (actually it might break everything), but you’ve got starting point to tweak it to your needs. Read more about preg_replace() in PHP manual.

Marko Dugonjić is a designer specialized in user experience design, web typography and web standards. He runs a nanoscale user interface studio Creative Nights and organizes FFWD.PRO, a micro-conference and workshops for web professionals.

Interested in more content like this?