49

In my website, users can put an URL in their profile.

This URL can be http://www.google.com or www.google.com or google.com.

If I just insert in my PHP code <a href="$url">$url</a>, the link is not always absolute.

How can I force the a tag to be absolute ?

StephanieQ
  • 872
  • 1
  • 13
  • 21
Arnaud
  • 4,884
  • 17
  • 54
  • 85
  • 2
    Make sure it's always prefixed with `http://` (or `//` for a protocol-relative link) – Pekka Jun 17 '13 at 12:09
  • 1
    You can try to use window.location But the best is to validate the users' input. Prompt them to provide a valid url. – ysrb Jun 17 '13 at 12:11

3 Answers3

68

If you prefix the URL with // it will be treated as an absolute one. For example:

<a href="//google.com">Google</a>.

Keep in mind this will use the same protocol the page is being served with (e.g. if your page's URL is https://path/to/page the resulting URL will be https://google.com).

  • You should also mention that if the absolute URI is specified with the two forward slashes only, then the protocol of the current site will be used when connecting to the profile url, i.e. https://yoursite.net will result in https://google.com, while http://yoursite.net will result in http://google.com – Edward Comeau Oct 04 '16 at 16:26
  • Yes, which suits the case in the question. By the way your links are broken (parsed). – Marco Chiappetta Oct 05 '16 at 16:39
  • 3
    This does not work if your website protocol is different from the url protocol. If your website is https then http url will not work because it will be given https instead. – DeepBlue Jan 18 '17 at 20:54
  • What if this field also stores emails? How will // react to that? What would be the solution to include emails as well? – Umar AlFarooq Jul 19 '22 at 13:16
11

Use a protocol, preferably http://

<a href="http://www.google.com">Google</a>

Ask users to enter url in this format, or concatenate http:// if not added.

If you prefix the URL only with //, it will use the same protocol the page is being served with.

<a href="//google.com">Google</a>
sinhayash
  • 2,693
  • 4
  • 19
  • 51
9

I recently had to do something similar.

if (strpos($url, 'http') === false) {
    $url = 'http://' .$url;
}

Basically, if the url doesn't contain 'http' add it to the front of the string (prefix).

Or we can do this with RegEx

$http_pattern = "/^http[s]*:\/\/[\w]+/i";

if (!preg_match($http_pattern, $url, $match)){  
   $url = 'http://' .$url;
}  

Thank you to @JamesHilton for pointing out a mistake. Thank you!

StephanieQ
  • 872
  • 1
  • 13
  • 21
  • 1
    This will always return false because if it finds http at the beginning of $url, it will return a 0 because the position of the string 'http' starts at index 0. 0 is considered falsey in the if statement, so you need to use the === operator instead. – James Hilton Jan 09 '17 at 19:15
  • @JamesHilton Aw jeez how embarrassing! Fixed, thank you! – StephanieQ Jan 12 '17 at 22:18
  • 2
    Maybe it would be a better RegExp if you use: `"/^https?:\/{2}\w/i"` – eddy85br Nov 13 '17 at 19:53