mod_rewrite 和相关 HTML 链接

发布于 2024-12-05 02:07:18 字数 765 浏览 2 评论 0原文

我编写了一个简单的 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.combar.com/baz/qux/ 所以我真的不能使用它。

任何帮助将不胜感激。谢谢!

I wrote a simple mod_rewrite to convert my ugly URLs:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2024-12-12 02:07:18

您还可以在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文