htaccess的重写打破相对路径 [英] htaccess rewrite breaks relative paths
问题描述
我有地图
http://www.myserver.com/home/
到
HTTP的htaccess文件://www.myserver。 COM /的index.php?节=家庭
这部分工作正常。我现在面临的问题是,我所有的图像和CSS驻留在子文件夹命名为资产,即
http://www.myserver.com/assets/images/
http://www.myserver.com/assets/css/
等。
重定向浏览器将查找文件在
http://www.myserver.com/home/assets/images/
在 这导致物破裂,因为这不是有效的路径。
我已经能够添加映射以上到正确的子文件夹中其他改写,但是,萤火显示图像居住在:
http://www.myserver.com/home /资产/图像/
我知道这是不是一个真正的问题,毕竟,我的图片和CSS加载就好了这一规则。 我只是好奇,我怎么可能做证明是实际的路径,这是路径:
http://www.myserver.com/assets/images/
粘贴下面我htaccess文件。非常感谢你提前。
选项+了FollowSymLinks
RewriteEngine叙述上
重写规则^([^ /] +)/资产/(CSS |图片| JS)(。*)/ $ /资产/ $ 2 / $ 3 [NC,L]
重写规则^([^ /] +)/ $ /index.php?section=$1 [NC,L]
现在的问题是,你没有考虑到相对URL的基本URI是HTML文档的参考用在的URI上解决。所以,相对URI路径像资产/图像/
与URI路径的HTML文档 /主页/
解析为 /主页/资源/图像/
而不是 /资产/图像/
。
可以不与mod_rewrite的改变此作为URI解析是由客户端,而不是由服务器完成的。唯一的解决方案是:
- 使用
更改基本URI BASE
元素(注意,这会影响的所有的相对URI); - 在使用绝对URI路径,例如
/资产/图像/
,而不是一个相对资产/图像/
; - 调整相对URI路径,所以在
引用的/ home /
调整到../资产/图像/
以反映路径的深度。
I have an htaccess file which maps
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]
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/
.
You cannot change this with mod_rewrite as URI resolution is done by the client and not by the server. The only solutions are:
- 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 relativeassets/images/
; - adjusting the relative URI path, so references in
/home/
are adjusted to../assets/images/
to reflect the path depth.
这篇关于htaccess的重写打破相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!