Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
823 views
in Technique[技术] by (71.8m points)

.htaccess - htaccess rewrite breaks relative paths

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 relative assets/images/;
  • adjusting the relative URI path, so references in /home/ are adjusted to ../assets/images/ to reflect the path depth.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...