상세 컨텐츠

본문 제목

How to get subdomain URL

Code Snippets/PHP

by w3labkr 2021. 12. 9. 07:52

본문

Here's a one-liner to get the subdomain:

$subdomain = join('.', explode('.', $_SERVER['HTTP_HOST'], -2))

explode with the limit parameter of -2 will split the string on each dot, ignoring the last two elements. If there are two or fewer elements, it returns an empty array.

 

join will assemble the resulting array back into a dot delimited string. In case you have multiple subdomains set, e.g. foo.bar.domain.com will return foo.bar.

 

If there is no subdomain, it will return an empty string.

 

Trying to get subdomain URL using php

 

Trying to get subdomain URL using php

Actually I am trying to get the sub-domain URL using php. I write code below: $sub_url = explode('.', $_SERVER['HTTP_HOST']); $suburl = $sub_url[0]; For Example: if sub domain URL is like my.exam...

stackoverflow.com

 

'Code Snippets > PHP' 카테고리의 다른 글

How to get site URL protocol?  (0) 2021.12.09

관련글 더보기