How to add http:// if it doesn’t exists in the URL in PHP?

There are many approaches to add http:// in the URL if it doesn’t exist. Some of them are discussed below.
Method 1: Using preg_match() function: This function searches string for pattern and returns true if pattern exists, otherwise returns false. Usually, the search starts from the beginning of the subject string. The optional parameter offset is used to specify the position from where to start the search.

Syntax:

int preg_match($pattern, $string, $pattern_array, $flags, $offset )

Return value: It returns true if pattern exists, otherwise false.



Example 1: This example takes the URL without http:// and returns the complete URL.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Function to add http
function addHttp($url) {
      
    // Search the pattern
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
          
        // If not exist then add http
        $url = "http://" . $url;
    }
      
    // Return the URL
    return $url;
}
  
// Declare a variable and initialize 
// it with URL
$url = "geeksforgeeks.org";
  
// Display URL
echo $url;
echo "\n";
  
// Display URL with http
echo addHttp($url);
  
?>

chevron_right


Output:

geeksforgeeks.org
http://geeksforgeeks.org

Example 2: This example takes the URL with http:// and returns the URL without doing any correction.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Function to add http
function addHttp($url) {
      
    // Search the pattern
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
          
        // If not exist then add http
        $url = "http://" . $url;
    }
      
    // Return the URL
    return $url;
}
  
// Declare a variable and initialize 
// it with URL
  
// Display URL
echo $url;
echo "\n";
  
// Display URL with http
echo addHttp($url);
  
?>

chevron_right


Output:

http://geeksforgeeks.org
http://geeksforgeeks.org

Method 2: This method use parse_url() function to add http:// if it does not exist in the url.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare a variable and initialize
// it with URL
$url = "geeksforgeeks.org";
  
// Display URL
echo $url;
echo "\n";
  
// Use parse_url() function
// to parse the URL
$parsed = parse_url($url);
  
// Check if parsed URL is empty
// then add http
if (empty($parsed['scheme'])) {
    $url = 'http://' . ltrim($url, '/');
}
  
// Display the URL
echo $url;
  
?>

chevron_right


Output:

geeksforgeeks.org
http://geeksforgeeks.org

Method 3: This method use strpos() function to add the http:// if it does not exist in the url.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare a variable and
// initialize it
$url = "geeksforgeeks.org";
  
// Display the url
echo $url;
echo "\n";
  
// Check http:// exist in the url
// or not if not exist the add it
if(strpos($url, 'http://') !== 0) {
    $url = 'http://' . $url;
}
  
// Display the url
echo $url;
  
?>

chevron_right


Output:

geeksforgeeks.org
http://geeksforgeeks.org

Method 4: This method use parse_url() function to add http:// in the url if it does not exists.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare a variable and
// initialize it
$url = "geeksforgeeks.org";
  
// Display the url
echo $url;
echo "\n";
  
// Function to add http in url
function addHttp($url, $scheme = 'http://') {
    return parse_url($url, PHP_URL_SCHEME) === null ?
            $scheme . $url : $url;
}
  
// Display URL
echo addHttp($url);
  
?>

chevron_right


Output:

geeksforgeeks.org
http://geeksforgeeks.org


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.