Auto-prepending links in RSS feeds with your domain (on Radiant)

When you syndicate Radiant content, you may hit the problem of internal links, or img src attributes pointing to invalid paths on a syndicator's domain. So, for example, an aggregator may link to http://aggregator.com/about-us instead of http://your-site.com/about-us. The simple fix is ofcourse to use absolute URLs for all your href and src attributes, but that is not default behavior in Radiant (or most CMSes as far as i know).

If you are using paperclipped, and use <r:asset /> tags to display images, you could fix their src attributes in script console or via the Settings extension by doing:

Radiant::Config["assets.url"] = "http://your-site.com/:class/:id/:basename:no_original_style.:extension"

But that still leaves you with the fact that <r:url />, <r:link /> and some other radius tags output relative urls. Heck, you may even have a few clients that are smart enough to write urls by hand! Here's a method that will convert all relative URLs to absolute ones;

def fix_for_syndication(text, host)   text.gsub(/href=('|")([^(http:|mailto:)].*?)('|")/,
    'href="' + host + '\2"').gsub(/src=('|")([^http:].*?)('|")/, 'src="' +
    host + '\2"') end

From there you can make a simple radius tag to use where-ever you output stuff to an RSS feed;

tag 'fix_links_for_syndication' do |tag|   fix_for_syndication(tag.expand,
    "#{tag.globals.page.request.protocol}#{tag.globals.page.request.host_with_port}")
    end

I used this in one of our own extensions (works-for-us'ish), use at your own risk; probably not compatible with the standard 'blog' extension for example.

In a reading mood?

I am in no way responsible for any sillyness going on below this line:

undefined tag `gravatar'

you just saved my day, thanks!

Have something to add?



  • (required, but not displayed)

  • (optional)

About this article

You’re reading an entry titled “Auto-prepending links in RSS feeds with your domain (on Radiant)”. Written

undefined tag `time_ago_in_words'
ago (11/09/2009) — and archived under absolute-urls, rss. So far, 1 people (dis)agreed with me, what do you think?

What the fuzz is all about

  1. 2 pages are tagged with WYM
  2. 1 page is tagged with absolute-urls
  3. 1 page is tagged with arrrrcamp
  4. 2 pages are tagged with documentation
  5. 2 pages are tagged with editor
  6. 2 pages are tagged with extension
  7. 2 pages are tagged with extensions
  8. 1 page is tagged with minitorial
  9. 1 page is tagged with navigation
  10. 10 pages are tagged with radiant
  11. 4 pages are tagged with rails
  12. 2 pages are tagged with ruby
  13. 2 pages are tagged with shell
  14. 1 page is tagged with sifr
  15. 2 pages are tagged with troubleshooting

Github activity

More places where you can find me

Back to top