Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-15T12:47:50.080Z
Answers.LastActivityDate: 2014-09-15T12:47:50.080Z
Answers.UserDisplayName: Fred Carlsen
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-15T16:56:47.950Z
Answers.Text: 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.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-15T16:58:26.733Z
Answers.Text: True. Didn't think about that. Don't be angry, Brad.
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-15T17:27:33.637Z
Answers.Text: I'm only angry when I'm sober.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-15T23:14:27.460Z
Answers.Text: That Twig template ternary totally toppled my tribulations. Thanks!
Answers.UserDisplayName: shanecavaliere
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-17T17:25:22.447Z
Answers.Text: @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.
Answers.UserDisplayName: Fred Carlsen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-15T10:29:12.603Z
Answers.LastActivityDate: 2014-09-15T10:40:41.687Z
Answers.LastEditDate: 2014-09-15T10:40:41.687Z
Answers.UserDisplayName: Paul
Body: <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>
  
Tags: <templating>
Title: What's the best way to make sure external URLs aren't interpreted as relative links?
ThreadText: # 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:
  
  1. Make your own field-type, that handles the url in PHP and appends http:// if needed ([Create a plugin for that](http://buildwithcraft.com/docs/plugins/field-types))
  2. 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](http://sprout.barrelstrengthdesign.com/craft-plugins/link-field) 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, username_3.  
  > **username_3**: I'm only angry when I'm sober.  
  > **username_0**: That Twig template ternary totally toppled my tribulations. Thanks!  
  > **username_1**: @username_0: 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.
  
  
  ---
  
  