[英]URL rewriting : including source files to “index.php” from HTTP requests which don't really exist
I've got a small website at the URL localhost/site/
. 我在URL
localhost/site/
有一个小型网站。 It has got a .htaccess
file, a index.php
file and a folder to store images called img
它有一个
.htaccess
文件,一个index.php
文件和一个用于存储图像的文件夹img
.htaccess file .htaccess文件
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) index.php?url=$1 [QSA,L]
As the above code every url to localhost/site
will redirect to index.php
if the url isn't a directory or a file. 如上代码所示,如果该URL不是目录或文件,则指向
localhost/site
每个URL都将重定向到index.php
。
index.php file index.php文件
<html>
<body>
<img src="img/logo.png"/>
</body>
</html>
Problem 问题
When I go to localhost/site
or localhost/site/[any_text]
, it works fine, which is index.php
will successfully load the file img/logo.png
. 当我转到
localhost/site
或localhost/site/[any_text]
,它工作正常,即index.php
将成功加载文件img/logo.png
。
But if I go to localhost/site/[any_text]/[any_text]
or further to any sub-directories in localhost/site
(which really don't exit, only a request) the index.php
will not load img/logo.png
但是,如果我转到
localhost/site/[any_text]/[any_text]
或更远的位置到localhost/site
任何子目录(实际上不会退出,只有一个请求), index.php
将不会加载img/logo.png
So why is that ? 那为什么呢?
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
. 因此,如果您正在查看
http://example.com/folder/page.html
则相对图像路径img/logo.png
将使客户端请求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
. 如果您改为插入前导斜线,则会告诉客户端该图像相对于站点根目录而言,这将导致客户端请求文件
http://example.com/img/logo.png
。
The correct image html code will be: 正确的图片HTML代码将是:
<img src="/site/img/logo.png" />
<img src="/img/logo.png"/>
^ note the leading slash
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.