118

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
3
  • 1
    If you had, mozilla.org alone, how would you know if it should be, http, https or ftp? Commented May 4, 2010 at 0:25
  • 7
    @Anthony: he says he wants to add "http://" if there's no other protocol. Commented May 4, 2010 at 0:27
  • 3
    @Anthony But when the user types the url without http:// or anything, do you expect it to be ftp:// or something? Commented May 4, 2010 at 0:28

8 Answers 8

273

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 "<BR>"; $init = microtime(true); for( $i = 1; $i < 100000; $i++ ) { addhttp( $url ); } echo microtime(true) - $init;
What if url begins with '//'?
151

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()

4 Comments

This should be the selected answer - why roll your own when the language has native support? Nice work.
One improvement that could be made with this function is checking to see if the field has a value so it is not adding an http to an empty field.
This doesn't work with relative protocols. e.g. echo addScheme('//google.com');
@Taylan Yeah, you would have to ltrim($url, '/')
52

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.

Comments

5

The best answer for this would be something like this:

function addScheme($url, $scheme="http://" )
{
  return empty(parse_url($url)['scheme']) ? $scheme . ltrim($url, '/') : $url;
}
// Test below
$url = 'google.com';
echo addScheme($url,"ftp://"); // "https://google.com"
echo addScheme('google.com'); // "http://google.com"
echo addScheme('https://google.com'); // "https://google.com"

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

Comments

2

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.

2 Comments

i'de prefer a regex version :)
Don't be too quick on regex. Regex tends to be hard to read and it could introduce another problem/bug once the problem grows.
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.

1 Comment

This would match (ergo return true and if would evaluate to false) weird combinations.. like htps, fttps, fttp, htp, I guess.
0

nickf's solution modified:

function addhttp($url) {
    if (!preg_match("@^https?://@i", $url) && !preg_match("@^ftps?://@i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}

1 Comment

I believe ftps:// is also valid.
-1
<?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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.