相对地址和绝对地址

发布于 2024-12-01 10:00:54 字数 498 浏览 11 评论 0原文

我的问题涉及相对和绝对地址。

多年来我构建了一个框架,我所有的 php 网站都构建在该框架上。

每个页面都加载到顶级“index.php”文件中。 这很酷,因为我从不为获取资源的路径而苦苦挣扎。系统每个级别的每个资源都是从顶级索引文件引用的,因此路径始终相似:

ie (img src="images/sprites/xxxxx.jpg" ) 来自系统中的每个页面。

但现在我通过引入 mod_rewrite 毁了它。 mod rewrite 似乎混淆了我一直在使用的路径系统。所以是时候更新路径了。样式表和图像不再加载。

我应该如何解决这个问题?相对路径的加载速度真的比绝对路径快吗? PHP 中是否有某种方法可以找到服务器文件系统的根目录并将其分配给 $var,然后将该 $var 添加到图像路径?这比仅仅引用我的图像和内容更快吗?样式表为“http://site.com/images/xxxx.jpg”?

我现在有很多东西要学。请输入任何内容

My question regards relative and absolute addresses.

I have over the years built a framework, which all my php websites are built on.

Every single page is loaded in the top level "index.php" file.
This is cool, because I never struggle with paths to resources. Every single resource, at every single level of the system is referenced from the top level index file, so the paths are always similar:

ie (img src="images/sprites/xxxxx.jpg" )
from every single page in the system.

But now I've ruined it by introducing mod_rewrite.
mod rewrite seems to have confused the path system I've been using. So it's time for me to update the paths. Stylesheets and images no longer load.

How should I fix this? Is it true that relative paths are loaded faster than absolute?
isn't there some way in PHP to find the root of your server file system assign it to a $var and then prepend that $var to image paths? Is that any faster than just referencing my images & Stylesheets as "http://site.com/images/xxxx.jpg" ?

I have alot to learn now. Any input please

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

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

发布评论

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

评论(3

暮光沉寂 2024-12-08 10:00:54

我应该如何解决这个问题?

使用以 / 开头的 URI,以便它们相对于当前域的根(保持相同的协议和端口)。

相对路径的加载速度真的比绝对路径快吗?

它们更短,因此节省了一些字节,所以从技术上来说——是的。但这并不是一个显着的节省。

PHP 中是否有某种方法可以找到服务器文件系统的根目录并将其分配给 $var,然后将该 $var 添加到图像路径?

是的,但是上面的技术更简单。

How should I fix this?

Use URIs that start with a / so they are relative to the root of the current domain (keeping the same protocol and port).

Is it true that relative paths are loaded faster than absolute?

They are shorter, so they save a few bytes, so technically — yes. This isn't a significant saving though.

isn't there some way in PHP to find the root of your server file system assign it to a $var and then prepend that $var to image paths?

Yes, but the above technique is simpler.

缱倦旧时光 2024-12-08 10:00:54

只需在 images 目录之前添加一个 / 即可,

img src="/images/sprites/xxxxx.jpg"

其效果与以下内容相同:

http://site.com/images/sprites/xxxxx.jpg

Just add a / before the images directory

img src="/images/sprites/xxxxx.jpg"

it's the same as:

http://site.com/images/sprites/xxxxx.jpg
私藏温柔 2024-12-08 10:00:54

$_SERVER["DOCUMENT_ROOT"] 将会满足您的要求:

<?php $root = $_SERVER["DOCUMENT_ROOT"]; ?>

但我同意其他发帖人的观点,即根相对路径(前导斜杠)是一个更简单的解决方案。

$_SERVER["DOCUMENT_ROOT"] will get you what you asked for:

<?php $root = $_SERVER["DOCUMENT_ROOT"]; ?>

But I agree with the other posters that root relative (leading slash) paths is a simpler solution.

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