使用mod_rewrite重写站点根目录
我试图使用 mod_rewrite 重写前面带有正斜杠 (/) 的相对链接以引用站点根目录。
我有 site:
http://localhost/mysite/
并且我有很多引用,例如在我的 css 目录中,格式如下:
background: url('/img/background.jpg');
我想使用 mod_rewrite 来指向:
http://localhost/mysite/img/background.jpg
但现在,它指向:
http://localhost/img/background.jpg
我如果这是理所当然的事,请提前道歉,但我是 mod_rewrite 的新手,到目前为止我还没有成功地让它发挥作用!
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:
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可不是什么好事。这是一个应该从根本上解决的错误。
例如,通过这种方式重写 URL,您可能会面临破坏
localhost
上其他站点中的链接的风险,而且处理站点根目录之外的 URL 是一项长期的维护噩梦。考虑在 CSS 中使用相对链接,例如,
如果图像位于 CSS 样式表所在的同级目录中。
样式表中的链接始终相对于样式表所在的位置 - 而不是使用它的 HTML 文件。这使得在样式表中使用相对链接变得非常容易。
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.
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.