Web开发 - 相对URL而无需复制文件
-
11-10-2019 - |
题
我有一个网站 index.php
在根文件夹中,图像在 /img
, 和 overview.php
在 /content
. 。我有一个 sidebar.php
两者中包含的文件 index.php
和 overview.php
. 。我应该如何参考 /img/image.gif
如果我在每个文件中包括一个链接?
的位置 image.gif
相对于引用它的文件的位置而变化。
使用 /img/image.gif
在 sidebar.php
将工作 index.php
, ,但对于位于 /content/overview.php
.
我看到的唯一解决方案是包括一个单独的 sidebar.php
在每个子目录中,或包括 /img
每个子目录中的目录。
我能找到的最好的建议是使用 <base
>在此处建议的HTML标签:
更改PHP中包含的内容的相对链接路径
但是,在同一链接中,萨姆古迪(Samgoody)建议 <base
>标签 is no longer properly supported in Internet Explorer, since version 7.
在承诺采取行动之前,我希望对此事有所了解。
谢谢。
编辑:我在下面使用错误的方法 "../"
例子-
root/index.php:
...
<link rel="stylesheet" type="text/css" href="style.css" />
<title>title</title>
</head>
<body>
<?php include('include/header.php'); ?>
<?php include('include/menu.php'); ?>
...
root/include/header.php:
...
<div id="header">
<span class="fl"><img src="img/dun1.png"/></span><span class="fr"><img src="img/dun2.png"/></span>
...
root/content/oferview.php:
...
<link rel="stylesheet" type="text/css" href="../style.css" media="screen" />
<title>Overview</title>
</head>
<body>
<?php include('../include/header.php'); ?>
<?php include('../include/menu.php'); ?>
...
解决方案
在sidebar.php中使用 /img/image.gif将在index.php中使用,但对于位于 /content/overview.php的文件失败
但这不应该。前面 /
使其成为一个绝对的路径,可以从服务器上的任何点起作用。如果这对您不起作用,那么某个地方就存在问题 - 在这种情况下,发布一些示例。
除非您计划有一天将整个站点移至子目录中,或者将图像移至内容交付网络(这两个操作都需要重新编写地址),则可以安全地使用绝对URL。