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!
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.
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.
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.
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.
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With