Web 开发 - 相对 URL,无需重复文件

发布于 2024-10-12 00:35:19 字数 1900 浏览 7 评论 0 原文

我有一个网站,根文件夹中包含 index.php ,图像位于 /img 中,overview.php 位于 /content< /代码>。我有一个 sidebar.php 文件,它包含在 index.phpoverview.php 中。如果我在每个文件中包含链接,我应该如何引用 /img/image.gif
image.gif 的位置相对于引用它的文件的位置发生变化。
sidebar.php 中使用 /img/image.gif 将在 index.php 中工作,但对于位于 的文件则失败/content/overview.php.
我能看到的唯一解决方案是在每个子目录中包含一个单独的 sidebar.php ,或者在每个子目录中包含一个 /img 目录。
我能找到的最好的建议是使用 >这里建议的 HTML 标签:
更改 PHP 中包含内容的相对链接路径< /a>

然而,在同一个链接中,SamGoody 建议 > 自版本 7 起,Internet Explorer 不再正确支持 标记。

在采取行动之前,我希望对此事有一些了解。

谢谢。

编辑:我使用下面的错误方法 "../"

示例 -

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/overview.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'); ?>
...

I have a site with index.php in the root folder, images in /img , and overview.php in /content. I have a sidebar.php file that is included in both index.php and overview.php. How should I refer to /img/image.gif if I include a link in each file?
The location of image.gif changes relative to the location of the file that references it.
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php.
The only solution that I can see is to either include a separate sidebar.php in each sub-directory, or include an /img directory in every sub-directory.
The best suggestion that I can find is to use the <base> HTML tag as suggested here:
Change relative link paths for included content in PHP

However, in the same link, SamGoody suggests that the <base> tag is no longer properly supported in Internet Explorer, since version 7.

I'd like some insight on the matter before committing to a course of action.

Thanks.

EDIT: I am using the wrong approach below with "../"

Example-

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/overview.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'); ?>
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦妄 2024-10-19 00:35:19

在 sidebar.php 中使用 /img/image.gif 将在 index.php 中工作,但对于位于 /content/overview.php 的文件则失败

但它不应该。前面的 / 使其成为绝对路径,可以从服务器上的任何位置工作。如果这对您不起作用,则说明某处存在问题 - 在这种情况下,请发布一些示例。

除非您计划有一天将整个站点移动到子目录中,或将图像移动到内容交付网络(这两种操作都需要重写地址),否则您可以安全地使用绝对 URL。

Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php

But it shouldn't. The preceding / makes it an absolute path which will work from any point on the server. If this doesn't work for you, there's a problem somewhere - in that case, post some examples.

Unless you are planning to move the whole site into a sub-directory one day, or move images to a Content Delivery Network (both actions would require re-writing the addresses) you can safely use absolute URLs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文