# Absolutize URLs in a string

**URL:** <https://www.ruby-forum.com/t/absolutize-urls-in-a-string/62432>\
**Category:** Rails\
**Created:** [June 4, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/absolutize-urls-in-a-string/62432 "2006-06-04T19:55:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julian\_SJulikS\_Tark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@Julian\_SJulikS\_Tark](https://www.ruby-forum.com/u/Julian_SJulikS_Tark)\
**Post date:** [June 4, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/absolutize-urls-in-a-string/62432/1 "2006-06-04T19:55:19Z")

</div>

## I wonder - do we have some helper/processor/gem to automatically convert all URLs in a passed string to their canonical equivalent - i.e. with the protocol, host and such prepended based on the Rails environment. Super-duper infty for RSS feeds (I hate their requirement for canonical URLs everywhere).

Julian ‘Julik’ Tarkhanov  
please send all personal mail to  
me at julik.nl

---

<div class="post-metadata">

**Author:** ![Julian\_SJulikS\_Tark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@Julian\_SJulikS\_Tark](https://www.ruby-forum.com/u/Julian_SJulikS_Tark)\
**Post date:** [June 4, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/absolutize-urls-in-a-string/62432/2 "2006-06-04T20:17:16Z")

</div>

Julian ‘Julik’ Tarkhanov wrote:

> ## I wonder - do we have some helper/processor/gem to automatically convert all URLs in a passed string to their canonical equivalent - i.e. with the protocol, host and such prepended based on the Rails environment. Super-duper infty for RSS feeds (I hate their requirement for canonical URLs everywhere).
> 
> Julian ‘Julik’ Tarkhanov  
> please send all personal mail to  
> me at julik.nl

I’m not sure about strings, but the url\_helpers have an :only\_path  
parameter that should do what you want. The following line will give a  
url with host protocol and port.

url\_for :action =\> ‘foo’, :only\_path =\> false

You can even set up your own defaults for the url helpers. Add the  
default\_url\_options method to your application\_controller

def default\_url\_options(options)  
{:only\_path =\> false}  
end

All urls in your entire app will now be absolute, unless you pass in  
:only\_path=\>true when creating the url

---

<div class="post-metadata">

**Author:** ![Julian\_SJulikS\_Tark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@Julian\_SJulikS\_Tark](https://www.ruby-forum.com/u/Julian_SJulikS_Tark)\
**Post date:** [June 4, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/absolutize-urls-in-a-string/62432/3 "2006-06-04T21:16:41Z")

</div>

On 4-jun-2006, at 20:17, Alex W. wrote:

> I’m not sure about strings, but the url\_helpers have an :only\_path  
> parameter that should do what you want. The following line will  
> give a  
> url with host protocol and port.

I am talking about URLs typed in by users, and relating to the site  
root - as in a Markdown-formatted entry:

```
We are proud to announce our new [bodily parts enlargment

```

contraption](/products/magic-device).

After being processed with Markdown I would like to process all href=  
and src= attributes so that they include the host, port and protocol.

–  
Julian ‘Julik’ Tarkhanov  
please send all personal mail to  
me at julik.nl

---

<div class="post-metadata">

**Author:** ![Julian\_SJulikS\_Tark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@Julian\_SJulikS\_Tark](https://www.ruby-forum.com/u/Julian_SJulikS_Tark)\
**Post date:** [June 7, 2006, 11:37am UTC](https://www.ruby-forum.com/t/absolutize-urls-in-a-string/62432/4 "2006-06-07T11:37:29Z")

</div>

On 4-jun-2006, at 21:14, Julian ‘Julik’ Tarkhanov wrote:

> I am talking about URLs typed in by users, and relating to the site  
> root - as in a Markdown-formatted entry:

Ok, thanks to some ruby-foo of Mauricio F. 😛 I don’t have to  
write this myself, all rejoice

[http://eigenclass.org/hiki.rb?Turning+Hiki+into+a+simple+CMS](http://eigenclass.org/hiki.rb?Turning+Hiki+into+a+simple+CMS)

require ‘cgi’  
require ‘uri’

def rss\_make\_content\_relative\_to(htmlcontent, root)  
htmlcontent.gsub(/(\<(img|a)\b[^\>]_(src|href)=)(["'])(._?)\4/) do  
md = $~

```
 url = URI.parse CGI.unescapeHTML(md[5])

 if url.relative? && url.path !~ /^\//
   md[1] + md[4] + root + "/" + md[5] + md[4]
 else
   md.to_s
 end

```

end  
end

–  
Julian ‘Julik’ Tarkhanov  
please send all personal mail to  
me at julik.nl
