1

我在 URL 上有一个小网站localhost/site/。它有一个.htaccess文件,一个index.php文件和一个用于存储图像的文件夹,称为img

.htaccess 文件

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+) index.php?url=$1 [QSA,L]

正如上面的代码,如果 url 不是目录或文件,每个 urllocalhost/site都会重定向到。index.php

index.php 文件

<html>

<body>
    <img src="img/logo.png"/>
</body>

</html>

问题

当我转到localhost/siteorlocalhost/site/[any_text]时,它工作正常,这index.php将成功加载文件img/logo.png

但是,如果我进入或进一步进入(实际上不退出,只有一个请求)中的localhost/site/[any_text]/[any_text]任何子目录,则不会加载localhost/siteindex.phpimg/logo.png

那为什么?

4

2 回答 2

1

This occurs because images that are included without leading slash or another form of locator like a protocol and domain will make the client assume that the path is relative to the currently viewed path. Thus if you are viewing http://example.com/folder/page.html the relative image path img/logo.png will make the client request http://example.com/folder/img/logo.png.

If you instead insert a leading slash you will tell the client that the image is located relative to the root of the site and it will instead result in the client requesting the file http://example.com/img/logo.png.

The correct image html code will be:

<img src="/site/img/logo.png" />
于 2013-01-03T12:05:00.500 回答
0
<img src="/img/logo.png"/>
          ^ note the leading slash
于 2013-01-03T11:59:50.717 回答