如何按照url的写法改写图片路径?
How to rewrite the images path according to the written rules of the url?
我正在尝试使用参数重写 url 应用程序。
我在主文件 main.php
.
中有一个 link
<a href="index?id=100&title=sub-url-here>Continue</a>
在 index.php
文件中,有一个图像标签,其 src 指向 ìmages/
文件夹。
<img src="images/fileaname.png" />
一切正常。
写入URL后出现问题。
这是我在 .htaccess
文件
中的内容
RewriteEngine O
RewriteRule ^index/([0-9]+)/([0-9a-zA-Z-_]+)/?$ index.php?id=&title= [NC,L]
我现在在 index.php
文件中有:
<a href="index/100/sub-url-here/">Continue</a>
但是,main.php
文件中的图像路径现在指向
<img src="100/sub-url-here/images/fileaname.png" />
如何根据.htaccess
中写的url重写图片路径?
问题是您对图像使用了相对 URLs。 浏览器 相对于浏览器地址栏中的当前 URL 解析这些内容(除非您有一个另外说明的 <base>
标记)。所以,在浏览器中查看URL/index/100/sub-url-here/
页面时,相对URLimages/filename.png
自然会解析为index/100/sub-url-here/images/filename.png
,而不是像你这样/images/filename.png
也许期待。
您需要使用相对于文档根目录的相对根目录 URLs(以斜杠开头),或使用相对于图像的绝对 URLs(使用方案+主机名)(和其他静态资源)- 因此图像 URL 与显示的页面 URL 无关。
例如:
<!-- https://example.com/images/filename.png -->
<img src="/images/filename.png">
另请参阅网站管理员堆栈上的以下问题,该问题详细介绍了在 URL-重写时使用相对 URL-路径:
我正在尝试使用参数重写 url 应用程序。
我在主文件 main.php
.
<a href="index?id=100&title=sub-url-here>Continue</a>
在 index.php
文件中,有一个图像标签,其 src 指向 ìmages/
文件夹。
<img src="images/fileaname.png" />
一切正常。
写入URL后出现问题。
这是我在 .htaccess
文件
RewriteEngine O
RewriteRule ^index/([0-9]+)/([0-9a-zA-Z-_]+)/?$ index.php?id=&title= [NC,L]
我现在在 index.php
文件中有:
<a href="index/100/sub-url-here/">Continue</a>
但是,main.php
文件中的图像路径现在指向
<img src="100/sub-url-here/images/fileaname.png" />
如何根据.htaccess
中写的url重写图片路径?
问题是您对图像使用了相对 URLs。 浏览器 相对于浏览器地址栏中的当前 URL 解析这些内容(除非您有一个另外说明的 <base>
标记)。所以,在浏览器中查看URL/index/100/sub-url-here/
页面时,相对URLimages/filename.png
自然会解析为index/100/sub-url-here/images/filename.png
,而不是像你这样/images/filename.png
也许期待。
您需要使用相对于文档根目录的相对根目录 URLs(以斜杠开头),或使用相对于图像的绝对 URLs(使用方案+主机名)(和其他静态资源)- 因此图像 URL 与显示的页面 URL 无关。
例如:
<!-- https://example.com/images/filename.png -->
<img src="/images/filename.png">
另请参阅网站管理员堆栈上的以下问题,该问题详细介绍了在 URL-重写时使用相对 URL-路径: