mod_rewrite 和相关 HTML 链接
我编写了一个简单的 mod_rewrite
来转换我丑陋的 URL
:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule news\/([^\/]+)/([0-9]+)-([^\/]+)$ news.php?id=$2
RewriteRule (.*)/images/(.*)$ images/$2
RewriteRule (.*)/css/(.*)$ css/$2
RewriteRule (.*)/js/(.*)$ js/$2
</IfModule>
问题立即出现,因为所有图像、脚本和样式都是相对于假来请求的> 网址
。因此,我必须为 regex
添加接下来的 3 个 end-of-line
断言,并添加接下来的 3 个规则。我想问的是,这种做法到底好不好,还是太消耗资源了?我知道这可以通过绝对链接来解决,但我的懒惰促使我尝试这个具有站点范围效果的解决方案。或者有更好的解决方案吗?
建议的一件事是在链接前添加斜杠。问题是该网站可以通过两个域访问:foo.com
和 bar.com/baz/qux/
所以我真的不能使用它。
任何帮助将不胜感激。谢谢!
I wrote a simple mod_rewrite
to convert my ugly URL
s:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule news\/([^\/]+)/([0-9]+)-([^\/]+)$ news.php?id=$2
RewriteRule (.*)/images/(.*)$ images/$2
RewriteRule (.*)/css/(.*)$ css/$2
RewriteRule (.*)/js/(.*)$ js/$2
</IfModule>
A problem immediately arose because all images, scripts, and styles are requested relative to the fake URL
. So I had to append the next 3 end-of-line
assertion for the regex
and add the next 3 rules. What I am asking is that whether this approach is good or is it consuming too much resources? I know that this can be solved via absolute links but my laziness prompted me to try this solution which has a site-wide effect. Or is there a better solution?
One thing that was suggested was prepending slashes to the links. The problem is that the site can be accessed via two domains: foo.com
and bar.com/baz/qux/
so I really can't use that.
Any help would be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还可以在 html 文件中添加
,这样可以节省 images/css/js 重写规则。关于“消耗太多资源”——当你获得非常多的访问权限时,它肯定会落在你的脚上。但我怀疑你是否会进入那个交通区域。
You could also add a
<base href=""/>
to your html files, which saves you the images/css/js rewrite rules.About "consuming too much resources" - when you get really really many accesses, it will fall on your feet for sure. But I doubt that you'll come into that traffic region.