使用php动态更改href值

时间:2012-04-02 02:58:59

标签: php header anchor href

我有这个问题,我需要更改锚点中的href的目录地址,我希望使用PHP使其工作..例如,如果我在主链接内我的href值将是“索引.php“但如果我在产品链接主页href值的文件夹内将更改为”../index.php“,因为标题只包含在每个页面..而且如果你可以为它建议任何替代品,我很高兴听到它..谢谢!

<div class="header_link">
     <a href="../"><div class="links"><p>Home</p></div></a>
     <a href="products/"><div class="links"><p>Products</p></div></a>
     <div class="links"><p>Packages</p></div>
     <div class="links"><p>Price List</p></div>
</div>

2 个答案:

答案 0 :(得分:3)

您应该使用绝对路径而不是相对路径。相对路径将变得难以管理,正如您所发现的那样,它们必须根据您正在查看的页面进行更改。

不要链接到../index.php,只需链接到/index.php即可。无论您在哪个页面,这都可以使用。

答案 1 :(得分:0)

尝试在html的<head>中使用基本标记。

<base href="http://www.mysite.com">

使所有href值绝对来自域。

<a href="/index.php">Home</a>