htaccess 重写破坏了相对路径

编程入门 行业动态 更新时间:2024-10-24 14:18:52
本文介绍了htaccess 重写破坏了相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个将

www.myserver/home/

映射到

www.myserver/index.php?section=home

这部分工作正常.我现在面临的问题是,我所有的图像和 css 都位于一个名为 assets 的子文件夹中,即

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.

www.myserver/assets/images/

www.myserver/assets/css/

重定向后浏览器会在

www.myserver/home/assets/images/

下寻找文件这会导致事情中断,因为这不是一条有效的路径.

which causes things to break, as this is not a valid path.

我已经能够添加另一个重写,将上述内容映射到正确的子文件夹,但是,Firebug 显示图像驻留在:

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:

www.myserver/home/assets/images/

我知道这不是真正的问题,毕竟,我的图像和 css 使用此规则加载得很好.我只是很好奇如何使路径显示为实际路径,即:

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:

www.myserver/assets/images/

在下面粘贴我的 htaccess 文件.事先非常感谢.

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 路径 /home/ 的 HTML 文档中的相对 URI 路径(如 assets/images/ 被解析为 /home/assets/images/> 而不是 /assets/images/.

The problem is that you didn’t consider that relative URLs are resolved on the base URI that is the URI of the HTML document the reference is used in. So a relative URI path like assets/images/ in an HTML document with the URI path /home/ is resolved to /home/assets/images/ instead of /assets/images/.

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

You cannot change this with mod_rewrite as URI resolution is done by the client and not by the server. The only solutions are:

  • 使用 BASE元素(注意这会影响所有相对URI);
  • 使用绝对 URI 路径,例如/assets/images/ 而不是相对的 assets/images/;
  • 调整相对 URI 路径,因此将 /home/ 中的引用调整为 ../assets/images/ 以反映路径深度.
  • change the base URI using the BASE element (note that this affects all relative URI);
  • using absolute URI paths, e.g. /assets/images/ instead of a relative assets/images/;
  • adjusting the relative URI path, so references in /home/ are adjusted to ../assets/images/ to reflect the path depth.

更多推荐

htaccess 重写破坏了相对路径

本文发布于:2023-11-01 23:07:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550674.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   路径   破坏了   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!