如何使用 php 获取我的站点域和托管?

How do I get my site domain and hosting using php?

这样我 link 到我在 html 和 php 中的文件。 因为通过重写 url,样式发生了变化,站点的外观变得丑陋。 但是这种方法不是很好,因为它在某些网站上不起作用。
我的代码示例:

<?php $domain = example.com ; $host = $_SERVER['HTTP-HOST'];?>

<link rel="stylesheet" href="<?php echo "http://$host/$domain" ?>style.css" >

难道一定是这样吗?

<?php
$domain = 'example.com';
$host = $_SERVER['HTTP_HOST'];
if ($host === 'localhost') {
    $host .= "/$domain";
}
?>

<link rel="stylesheet" href="<?php echo "http://$host/" ?>style.css" >

UPD:为 localhost 主机添加动态主机命名。