我在根目录中创建了一个Header.php
文件。
以下是树形结构。
-SiteName
-Header.php
-Books
-Samples
-Math
-MathBook.php
-index.php
-About.php
所以MathBook.php包括Header.php,如下所示
include '../../../Header.php';
Header.php包含在About.php中,就像这样
include 'Header.php';
现在Header.php包含导航菜单的代码。
<ul>
<li><a href="index.php" class="<?php active('index.php');?>">Home</a></li>
<li><a href="About.php" class="<?php active('AboutUs.php');?>">About Us</a></li>
</ul>
所以现在,当我在Math.php中,然后按回家时,我需要:
/Books/Samples/Math/index.php
显然网址不存在。
我如何确保用户在Math.php中时应该转到index.php
而不包括当前路径。
Header.php包含在所有文件中。
答案 0 :(得分:1)
您可以定义与您网站的基本网址相关的包含和链接。
href可以这样写:
<ul>
<li>
<a
href="<?php echo $_SERVER['HTTP_HOST'].'/index.php';?>"
class="<?php active('index.php');?>">Home</a>
</li>
<li>
<a
href="<?php echo $_SERVER['HTTP_HOST'].'/About.php';?>"
class="<?php active('AboutUs.php');?>">About Us</a>
</li>
</ul>
答案 1 :(得分:0)
您使用的是相对路径。
输入&#34; index.php&#34;在href属性中,您指向名为&#34; index.php&#34;的文件。在当前文件夹中。
如果要指向根文件夹中的文件,则需要使用斜杠/
href="/<sub folder if any>/index.php"
href="/<sub folder if any>/About.php"
这会将用户带到您想要的文件,如果您不确定要使用什么,只需使用这样的绝对路径:
href="https://www.example.com/<sub folder if any>/index.php"
href="https://www.example.com/<sub folder if any>/About.php"
修改强>
添加斜杠是有效的,因为通过添加/
,您可以告诉浏览器转到根文件夹/网站并从那里开始路径。
让我们说你在yourwebsite.com/current/path/file.html
。
/some/path/to/a-file.php
的链接已解析为yourwebsite.com/some/path/to/a-file.php
但some/path/to/a-file.php
的链接已解析为yourwebsite.com/current/path/some/path/to/a-file.php