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

时间:2015-10-14 11:14:29

标签: php html hyperlink href prefix

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

e.g。而不是将href="/css/style.css"更改为href="http://example.com/css/style.css"

我可以使用php在网站根目录中调用吗?

编辑:最初尝试使用Sass& Css,但似乎是错误的解决方法。

1 个答案:

答案 0 :(得分:1)

您可以使用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