Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get domain with its subdomain from url

I'm using this function to get domain and subdomain from a string. But If string is already my expected format, it returns null

function getDomainFromUrl($url) {
    $host = parse_url($url, PHP_URL_HOST);
    return preg_replace('/^www\./', '', $host);
}

$url = "http://abc.example.com/" -> abc.example.com | OK

$url = "http://www.example.com/" -> example.com | OK

$url = "abc.example.com" -> FAILS!
like image 831
hakkikonu Avatar asked May 13 '15 19:05

hakkikonu


People also ask

How do I point a subdomain to a URL?

Under Modify a Subdomain, locate the domain you want to redirect, then click its Manage Redirection link on the right. In the text box, type the URL you would like visitors to be redirected to if they go to the subdomain sample1.hgexample.com. Click Save. You will see the redirected URL under the Redirection column.

Is it possible to find all subdomains of a domain?

You can find DNS records using the nslookup command in the command line. It shows all DNS records for a given domain, including a list of subdomains. The command works for Windows, Linux, and macOS operating systems.

Is a subdomain a separate domain?

While a subdomain is part of the main website, it's considered a separate entity by search engines. People recognized this and decided to use subdomains to organize their website, without allowing certain parts of the site to be indexed by Google. Companies use subdomains for a variety of reasons.


2 Answers

That is because abc.example.com is not a PHP_URL_HOST so you need to first check that it is one first. So you should do something simple like this, where if the url is doesn't have a protocol -> add it:

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

function getDomainFromUrl($url) {
    $host = parse_url($url, PHP_URL_HOST);
    if($host){
        return preg_replace('/^www\./', '', $host);
    }else{
        //not a url with protocol
        $url = addhttp($url); //add protocol
        return getDomainFromUrl($url); //run function again.
    }
}
like image 59
maxisme Avatar answered Oct 13 '22 00:10

maxisme


Here's a pure regex solution:

function getDomainFromUrl($url) {
    if (preg_match('/^(?:https?:\/\/)?(?:(?:[^@]*@)|(?:[^:]*:[^@]*@))?(?:www\.)?([^\/:]+)/', $url, $parts)) {
        return $parts[1];
    }
    return false; // or maybe '', depending on what you need
}

getDomainFromUrl("http://abc.example.com/"); // abc.example.com

getDomainFromUrl("http://www.example.com/"); // example.com

getDomainFromUrl("abc.example.com");         // abc.example.com

getDomainFromUrl("[email protected]"); // abc.example.com

getDomainFromUrl("https://username:[email protected]"); // abc.example.com

getDomainFromUrl("https://username:[email protected]:123"); // abc.example.com

You can try it here: http://sandbox.onlinephpfunctions.com/code/3f0343bbb68b190bffff5d568470681c00b0c45c

In case you want to know more about the regex:

^                 matching must start from the beginning on the string
(?:https?:\/\/)?  an optional, non-capturing group that matches http:// and https://

(?:(?:[^@]*@)|(?:[^:]*:[^@]*@))?
                  an optional, non-capturing group that matches either *@ or *:*@ where * is any character
(?:www\.)?        an optional, non-capturing group that matches www.
([^\/:]+)          a capturing group that matches anything up until a '/', a ':', or the end of the string
like image 43
Schlaus Avatar answered Oct 13 '22 01:10

Schlaus