简体   繁体   English

如何使用“相对”网址?

[英]How to use “relative” URLs?

i have a doubt, in an HTML file I have the following structure: 我有一个疑问,在HTML文件中我有以下结构:

    MAIN -> index.php
         -> IMAGES -> image.jpg

In an 'img' tag should I use this: 在'img'标签中我应该使用这个:

    <img src="IMAGES/image.jpg">

Or this? 或这个?

    <img src="/IMAGES/image.jpg">

Note that in the second example I added the slash before IMAGES. 请注意,在第二个示例中,我在IMAGES之前添加了斜杠。 This is my question. 这是我的问题。

两者都适用于这种情况,但我建议使用您的第一个选项(相对路径),因此如果将来您将整个项目移动到新的根目录下,您的所有站点都将继续工作。

Both would work but if you don't expect to ever move your IMAGES directory, go with /IMAGES/image.jpg . 两者都可以工作,但如果你不想移动你的IMAGES目录,请使用/IMAGES/image.jpg This would be preferable because you'll be able to use that same uri anywhere in your markup (say, if you add MAIN/SCRIPTS/newscript.php , then /IMAGES.. will work, IMAGES/image.jpg would not). 这将是更好的,因为你将能够在你的标记中的任何地方使用相同的uri(例如,如果你添加MAIN/SCRIPTS/newscript.php ,那么/IMAGES..将起作用, IMAGES/image.jpg不会)。 If, however, you always intend to store IMAGES as a directory at the same level as index.php, but you might end up moving index.php somewhere else, then you might consider using IMAGES/image.jpg . 但是,如果您始终打算将IMAGES存储为与index.php相同级别的目录,但最终可能会将index.php移动到其他位置,那么您可以考虑使用IMAGES/image.jpg

In this case, they're exactly the same, but consider this updated example: 在这种情况下,它们完全相同,但请考虑这个更新的示例:

    MAIN -> index.php
         -> ABOUT -> index.php
         -> IMAGES -> image.jpg

Now, inside ABOUT/index.php there is a difference, because 现在,在ABOUT / index.php里面一个区别,因为

<img src="IMAGES/image.jpg"> # => /ABOUT/IMAGES/image.jpg

<img src="/IMAGES/image.jpg"> # => /IMAGES/image.jpg

The "safety" of using relative URLs depends on if you will be using subdirectories in your URLs. 使用相对URL的“安全性”取决于您是否将在URL中使用子目录。

Assuming your file structure, 假设你的文件结构,

If you are at http://example.com/index.php , then the url IMAGES/image01.jpg will work. 如果你在http://example.com/index.php ,那么网址IMAGES/image01.jpg就可以了。

If you are at http://example.com/somedir/index.php , then "IMAGES/image01.jpg will not load (It will be looking for http://example.com/somedir/IMAGES/image01.jpg instead of your intended http://example.com/IMAGES/image01.jpg ) 如果您在http://example.com/somedir/index.php ,那么"IMAGES/image01.jpg将无法加载(它将寻找http://example.com/somedir/IMAGES/image01.jpg而不是您想要的http://example.com/IMAGES/image01.jpg

This means that using relative URLs is "dangerous" if you: 这意味着如果您使用相对URL是“危险的”:

1) Move or copy your index.php file into a subdirectory 2) Use url rewriting to remove index.php from your URLs (and so hae URLs like example.com/some/location rewrite to example.com/index.php?l=some/location ) 1)将index.php文件移动或复制到子目录中2)使用url重写从URL中删除index.php(以及例如example.com/some/location重写到example.com/index.php?l=some/location URL) example.com/index.php?l=some/location

Your safest bet is to use a variable to get your base URL: 最安全的做法是使用变量来获取基本URL:

define('BASE_URL', 'http://example.com');

Then later in your HTML: 然后在你的HTML中:

<img src="<?php echo BASE_URL; ?>/IMAGES/image01.jpg" alt="" />

If you move your site in the future, you can change the BASE_URL constant. 如果将来移动站点,可以更改BASE_URL常量。

Both should work, but I'd suggest using the slash. 两者都应该有效,但我建议使用斜杠。 When you add the slash before hand, it means to look in the top most directory. 当您手动添加斜杠时,它意味着查看最顶层的目录。 With out it it means relative to the current file. 有了它,它意味着相对于当前文件。

If you use the method with the slash before hand it allows you to move the file, or copy html to other files and it won't matter where they are. 如果您使用斜杠前面的方法,它允许您移动文件,或将html复制到其他文件,它们在哪里都无关紧要。

On a side note, without the slash is called a relative path, and with the slash is called and absolute path. 在旁注中,没有斜杠称为相对路径,并且斜杠被称为绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM