如何使用 php 来确定 href 中的站点根目录

How to use php to determine the site root inside href

我希望能够使用 php 确定我网站的根目录,这样我就可以轻松地在相对链接和绝对链接之间切换以进行本地和在线测试。

例如而不是将 href="/css/style.css" 更改为 href="http://example.com/css/style.css"

我可以使用 php 调用站点根目录吗?

编辑:最初用 Sass 和 Css 尝试过此方法,但似乎是错误的方法。

您可以使用 PHP 等脚本语言轻松实现此目的。

在您的 html 页面顶部

<?PHP
    $pageRoot = "http://localhost:8080"; // for example
    // $pageRoot = "http://myUrl.com"; // commented out as we're local testing first
?>

然后调用您的 css 文件将 href 更改为:

href="<?=$pageRoot?>"/css/style.css"

然后在远程服务器

上简单地注释掉本地主机并取消注释正确的url