<p>There is a fieldtype called <a href="http://straightupcraft.com/craft-plugins/link-field" rel="nofollow">Link</a> from Sprout that validates links, and it works very well.</p>

<p>One point of criticism is that the error it gives you when you haven't added the protocol isn't very client-friendly. It just says: 'Link must be a valid link.'</p>

<p>You could either contact Sprout and give them feedback on that, and they'll probably improve it (it's in beta), or you could modify it yourself.</p>

CC BY-SA 3.0
2014-09-15T12:47:50.080Z
2014-09-15T12:47:50.080Z
Fred Carlsen
<p>Using Craft, you could go two ways:</p>

<ol>
<li><p>Make your own field-type, that handles the url in PHP and appends http:// if needed (<a href="http://buildwithcraft.com/docs/plugins/field-types">Create a plugin for that</a>)</p></li>
<li><p>Check the url for 'http' in your template, and if needed prepend it:</p>

<pre><code>{% set url = entry.url | slice(0,4) == 'http' ? entry.url : 'http://' ~ entry.url %}
</code></pre></li>
</ol>

<p>Be sure to use the {% cache %} tag if you use option 2, so the check only has to be done once.</p>

<p>Edit: You could use <a href="http://sprout.barrelstrengthdesign.com/craft-plugins/link-field">this plugin</a> to pre-validate your urls.</p>

CC BY-SA 3.0
2014-09-15T16:56:47.950Z
The {% cache %} tag is going to force a database call on every request to retrieve the cached item. It'll probably be faster to just let Twig/PHP do the processing each time.
Brad Bell
CC BY-SA 3.0
2014-09-15T16:58:26.733Z
True. Didn't think about that. Don't be angry, Brad.
Paul
CC BY-SA 3.0
2014-09-15T17:27:33.637Z
I'm only angry when I'm sober.
Brad Bell
CC BY-SA 3.0
2014-09-15T23:14:27.460Z
That Twig template ternary totally toppled my tribulations. Thanks!
shanecavaliere
CC BY-SA 3.0
2014-09-17T17:25:22.447Z
@shanecavaliere: I suggest you use the plugin, as that twig-code doesn't really validate. If your client messes up somehow, let's say miss an h at the start, the url will end up as http://ttp://url.com.
Fred Carlsen
CC BY-SA 3.0
2014-09-15T10:29:12.603Z
2014-09-15T10:40:41.687Z
2014-09-15T10:40:41.687Z
Paul
<p>I have a field where the client can enter a URL, and then that field's value is put into the 'href' of a link in the template. The client will sometimes prepend it with "http://", sometimes with "www.", and sometimes with nothing. Unless it's prepended with "http://" or "https://", the link is interpreted as relative and tries to go to "<a href="http://mysite.com/www.otherlink.com" rel="nofollow">http://mysite.com/www.otherlink.com</a>", which is obviously not what I want.</p>

<p>What's the best way of automatically handling different URL formats?</p>

<templating>
What's the best way to make sure external URLs aren't interpreted as relative links?
# What's the best way to make sure external URLs aren't interpreted as relative links?

(Asked by: username_0 on 2014-09-15)

I have a field where the client can enter a URL, and then that field's value is put into the 'href' of a link in the template. The client will sometimes prepend it with "http://", sometimes with "www.", and sometimes with nothing. Unless it's prepended with "http://" or "https://", the link is interpreted as relative and tries to go to "<http://mysite.com/www.otherlink.com>", which is obviously not what I want.

What's the best way of automatically handling different URL formats?

**Tags:** templating

---

## 2 answers

### Answer by username_2 on 2014-09-15. Score: 7 | Accepted answer

Using Craft, you could go two ways:

Make your own field-type, that handles the url in PHP and appends http:// if needed (Create a plugin for that)
Check the url for 'http' in your template, and if needed prepend it:
{% set url = entry.url | slice(0,4) == 'http' ? entry.url : 'http://' ~ entry.url %}


Be sure to use the {% cache %} tag if you use option 2, so the check only has to be done once.
Edit: You could use this plugin to pre-validate your urls.

> **Comments:**
> **username_3**: The {% cache %} tag is going to force a database call on every request to retrieve the cached item. It'll probably be faster to just let Twig/PHP do the processing each time.  
> **username_2**: True. Didn't think about that. Don't be angry, Brad.  
> **username_3**: I'm only angry when I'm sober.  
> **username_0**: That Twig template ternary totally toppled my tribulations. Thanks!  
> **username_1**: @shanecavaliere: I suggest you use the plugin, as that twig-code doesn't really validate. If your client messes up somehow, let's say miss an h at the start, the url will end up as http://ttp://url.com.  

---

### Answer by username_1 on 2014-09-15. Score: 3

There is a fieldtype called [Link](http://straightupcraft.com/craft-plugins/link-field) from Sprout that validates links, and it works very well.

One point of criticism is that the error it gives you when you haven't added the protocol isn't very client-friendly. It just says: 'Link must be a valid link.'

You could either contact Sprout and give them feedback on that, and they'll probably improve it (it's in beta), or you could modify it yourself.


---

