开发者

Relative and Absolute Addresses

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 b开发者_如何学JAVAy 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


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.


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


$_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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜