116

How can I add http:// to a URL if it doesn't already include a protocol (e.g. http://, https:// or ftp://)?

Example:

addhttp("google.com"); // http://google.com
addhttp("www.google.com"); // http://www.google.com
addhttp("google.com"); // http://google.com
addhttp("ftp://google.com"); // ftp://google.com
addhttp("https://google.com"); // https://google.com
addhttp("http://google.com"); // http://google.com
addhttp("rubbish"); // http://rubbish
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
CodeOverload
  • 45,400
  • 52
  • 128
  • 215

9 Answers9

271

A modified version of @nickf code:

function addhttp($url) {
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}

Recognizes ftp://, ftps://, http:// and https:// in a case insensitive way.

Alix Axel
  • 147,060
  • 89
  • 388
  • 491
  • 5
    Having compared addhttp and addscheme below, I've come to the conclusion that addscheme is better in terms of performance: `$url = "www.google.com"; $init = microtime(true); for( $i = 1; $i < 100000; $i++ ) { addScheme( $url ); } echo microtime(true) - $init; echo "
    "; $init = microtime(true); for( $i = 1; $i < 100000; $i++ ) { addhttp( $url ); } echo microtime(true) - $init; `
    – Luis Lobo Borobia May 13 '13 at 19:56
  • What if url begins with '//'? – holden321 Oct 25 '19 at 09:00
146

At the time of writing, none of the answers used a built-in function for this:

function addScheme($url, $scheme = 'http://')
{
  return parse_url($url, PHP_URL_SCHEME) === null ?
    $scheme . $url : $url;
}

echo addScheme('google.com'); // "http://google.com"
echo addScheme('https://google.com'); // "https://google.com"

See also: parse_url()

Dave Jarvis
  • 29,586
  • 38
  • 176
  • 304
Ja͢ck
  • 166,373
  • 34
  • 252
  • 304
51

Simply check if there is a protocol (delineated by "://") and add "http://" if there isn't.

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

Note: This may be a simple and straightforward solution, but Jack's answer using parse_url is almost as simple and much more robust. You should probably use that one.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Brenton Alker
  • 8,757
  • 3
  • 32
  • 37
2

The best answer for this would be something like this:

function addhttp($url, $scheme="http://" )
{
  return $url = empty(parse_url($url)['scheme']) ? $scheme . ltrim($url, '/') : $url;
}

The protocol flexible, so the same function can be used with ftp, https, etc.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
ThisDude
  • 91
  • 2
  • 9
1

Scan the string for ://. If it does not have it, prepend http:// to the string... Everything else just use the string as is.

This will work unless you have a rubbish input string.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Rosdi Kasim
  • 22,381
  • 23
  • 123
  • 149
0
<?php
    if (!preg_match("/^(http|ftp):/", $_POST['url'])) {
        $_POST['url'] = 'http://'.$_POST['url'];
    }
    $url = $_POST['url'];
?>

This code will add http:// to the URL if it’s not there.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
ayan
  • 11
  • 1
  • 2
0

Try this. It is not watertight1, but it might be good enough:

function addhttp($url) {
    if (!preg_match("@^[hf]tt?ps?://@", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}

1. That is, prefixes like "fttps://" are treated as valid.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
nickf
  • 520,029
  • 197
  • 633
  • 717
  • 6
    This would match (ergo return true and if would evaluate to false) weird combinations.. like htps, fttps, fttp, htp, I guess. – kamasheto May 04 '10 at 00:28
0

nickf's solution modified:

function addhttp($url) {
    if (!preg_match("@^https?://@i", $url) && !preg_match("@^ftps?://@i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
kamasheto
  • 1,042
  • 6
  • 12
0

You should use this NPM package: normalize-url

normalizeUrl(url, {
  removeTrailingSlash: false,
  stripWWW: false,
})
Julien Le Coupanec
  • 7,195
  • 6
  • 47
  • 60