How to add http:// if it doesn't exist in the URL
Asked Answered
S

8

117

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
Schaumberger answered 4/5, 2010 at 0:22 Comment(3)
If you had, mozilla.org alone, how would you know if it should be, http, https or ftp?Ilene
@Anthony: he says he wants to add "http://" if there's no other protocol.Breezeway
@Anthony But when the user types the url without http:// or anything, do you expect it to be ftp:// or something?Schaumberger
P
272

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.

Phony answered 4/5, 2010 at 0:30 Comment(2)
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; Downcome
What if url begins with '//'?Ovovitellin
R
149

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

Radiobroadcast answered 5/2, 2013 at 6:29 Comment(4)
This should be the selected answer - why roll your own when the language has native support? Nice work.Ultra
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.Kurbash
This doesn't work with relative protocols. e.g. echo addScheme('//google.com');Loleta
@Loleta Yeah, you would have to ltrim($url, '/')Parthenope
F
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.

Fussbudget answered 4/5, 2010 at 0:44 Comment(0)
T
4

The best answer for this would be something like this:

function addScheme($url, $scheme="http://" )
{
  return $url = 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.

Tory answered 29/7, 2017 at 2:33 Comment(0)
R
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.

Radiology answered 4/5, 2010 at 0:32 Comment(2)
i'de prefer a regex version :)Schaumberger
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.Radiology
B
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.

Breezeway answered 4/5, 2010 at 0:25 Comment(1)
This would match (ergo return true and if would evaluate to false) weird combinations.. like htps, fttps, fttp, htp, I guess.Berwick
B
0

nickf's solution modified:

function addhttp($url) {
    if (!preg_match("@^https?://@i", $url) && !preg_match("@^ftps?://@i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}
Berwick answered 4/5, 2010 at 0:30 Comment(1)
I believe ftps:// is also valid.Phony
D
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.

Darrendarrey answered 17/1, 2013 at 4:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.