Using mod_rewrite to rewrite site root
I'm trying to have relative links that are preceded with a forward slash (/) get rewritten with mod_rewrite to refer to the site root.
I have site:
http://localhost/mysite/
and I have numerous references, for example, in my css directory, formatted like such:
background: url('/img/background.jpg');
I would like to use mod_rewrite to point that at开发者_StackOverflow中文版:
http://localhost/mysite/img/background.jpg
But right now, it is pointing to:
http://localhost/img/background.jpg
I apologize in advance if this is a no-brainer, but I'm new to mod_rewrite, and I have so far been unsuccessful in getting this to work!
This is not a good thing to do. It is duct-taping a mistake that should be fixed at its core.
By rewriting URLs this way, you risk breaking links in other sites on localhost
, for example, and it is a long-term maintenance nightmare having to deal with URLs outside your site's root directory.
Consider using relative links in your CSS instead, e.g.
background: url('../img/background.jpg');
if the images are in a sibling directory of where your CSS style sheet is in.
Links in style sheets are always relative to the location the style sheet is in - not the HTML file that uses it. That makes it very easy to use relative links in style sheets.
精彩评论