gpt4 book ai didi

.htaccess - htaccess 重写破坏了相对路径

转载 作者:行者123 更新时间:2023-11-28 09:59:38 25 4
gpt4 key购买 nike

我有一个 htaccess 文件映射

http://www.myserver.com/home/
to
http://www.myserver.com/index.php?section=home

This part works fine. The issue I am facing now is, all my images and css reside in a sub-folder named assets, i.e.

http://www.myserver.com/assets/images/
http://www.myserver.com/assets/css/
etc.

After redirection the browser will look for the files under

http://www.myserver.com/home/assets/images/
which causes things to break, as this is not a valid path.

I have been able to add another rewrite that maps the above to the correct sub-folder, however, Firebug shows that the images are residing in:

http://www.myserver.com/home/assets/images/

I know it's not really a problem, after all, my images and css are loading just fine with this rule. I'm just curious as to how I could make the path shown to be the actual path, which is:

http://www.myserver.com/assets/images/

Pasting my htaccess file below. Thank you very much beforehand.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/]+)/assets/(css|images|js)/(.*)$ /assets/$2/$3 [NC,L]
RewriteRule ^([^/]+)/$ /index.php?section=$1 [NC,L]

最佳答案

问题是您没有考虑到相对 URL 是在基本 URI 上解析的,该基本 URI 是使用引用的 HTML 文档的 URI。所以相对 URI 路径如 assets/images/在具有 URI 路径 /home/ 的 HTML 文档中解析为 /home/assets/images/而不是 /assets/images/ .

您不能使用 mod_rewrite 更改此设置,因为 URI 解析是由客户端而不是服务器完成的。唯一的解决方案是:

  • 使用 BASE element 更改基本 URI (请注意,这会影响所有 相对 URI);
  • 使用绝对 URI 路径,例如/assets/images/而不是亲戚assets/images/ ;
  • 调整相对 URI 路径,因此在 /home/ 中引用调整为../assets/images/以反射(reflect)路径深度。

关于.htaccess - htaccess 重写破坏了相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24763316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com