使用 mod-rewrite 将 jquery.js 的请求重定向到 GoogleAPI 缓存

发布于 2024-09-02 14:33:09 字数 876 浏览 5 评论 0原文

我们的 Linux 服务器配备 Apache 2.x、Plesk 8.x,托管着许多电子商务网站。为了利用浏览器缓存,我们希望使用 Google 提供的 jquery.js 副本。

因此,在每个域的 vhost.conf 文件中,我们可以使用以下 RewriteRule

RewriteCond %{REQUEST_FILENAME} jquery.min.js [nc]
RewriteRule . http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js [L]

在 vhost_ssl.conf 中,

RewriteCond %{REQUEST_FILENAME} jquery.min.js [nc]
RewriteRule . https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js [L]

现在这些规则在每个域的各个 vhost.conf 文件中运行良好。然而,我们托管了 200 多个域,我希望它们能够工作,但似乎无法让它们在 httpd.conf 文件中全局工作。

挑战如下:

  1. 让重写规则在 httpd.conf 中工作
  2. 检测 HTTPS 是否打开,如果打开并且是安全页面,则重写为...
  3. 每个单独的域仍将有其自己的自定义 mod 重写规则。哪些规则优先 - 全局规则还是每个域规则?它们结合了吗?如果我在全局 httpd.conf 中添加“RewriteEngine On”指令,然后在 vhost.conf 中再次添加“RewriteEngine On”指令,可以吗?

请让我知道你们的建议是什么。迫切需要解决这个问题。

Our Linux server with Apache 2.x, Plesk 8.x hosts a number of e-commerce websites. To take advantage of browser caching we would like to use Google's provided copy of jquery.js.

Hence in the vhost.conf file of each we can use the following RewriteRule

RewriteCond %{REQUEST_FILENAME} jquery.min.js [nc]
RewriteRule . http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js [L]

And in vhost_ssl.conf

RewriteCond %{REQUEST_FILENAME} jquery.min.js [nc]
RewriteRule . https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js [L]

OK now these rules work fine in the individual vhost.conf files of each domain. However we host over 200 domains, I would like for them to work but cannot seem to get them to work globally in the httpd.conf file.

Challenges are the following:

  1. Get the rewriterule to work in httpd.conf
  2. Detect if HTTPS is on, and if it is and the is is a secure page, rewrite to ...
  3. Each individual domain will still have it's own custom mod-rewrite rules. Which rules take precedence - global or per-domain? Do they combine? Is it ok if I have the "RewriteEngine On" directive in the global httpd.conf and then again in the vhost.conf?

Please let me know what your guys' suggestions are. Desperate for a solution to this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

瞄了个咪的 2024-09-09 14:33:09

我同意 Gumbo 的观点,但你可以这样做:

1.. 让重写规则在 httpd.conf 中工作

正如

默认情况下,虚拟主机不会继承主服务器上下文中的 mod_rewrite 配置设置。要使主服务器设置应用于虚拟主机,您必须在每个部分中放置以下指令:

RewriteEngine 开启
RewriteOptions 继承

2..检测HTTPS是否开启,如果开启并且是安全页面,则重写为...

这可以通过查看 %{HTTP_HOST} 来完成:

RewriteCond %{HTTPS} !=on
RewriteRule /jquery\.min\.js$ https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js [NC,R=permanent]

RewriteCond %{HTTPS} =on
RewriteRule /jquery\.min\.js$ http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js [NC,R=permanent]

您还应该转义点,并且您使用的 RewriteCond 是不必要;您可以在 RewriteRule 中执行此操作。

3.. 每个单独的域仍将有其自己的自定义 mod 重写规则。哪些规则优先 - 全局规则还是每个域规则?它们结合了吗?如果我在全局 httpd.conf 中添加“RewriteEngine On”指令,然后在 vhost.conf 中再次添加“RewriteEngine On”指令,可以吗?

它将被添加到虚拟主机的 mod_rewrite 配置之前。

I agree with Gumbo, but here's how you can do it:

1.. Get the rewriterule to work in httpd.conf

As is clearly stated in the manual:

By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each section:

RewriteEngine On
RewriteOptions Inherit

2.. Detect if HTTPS is on, and if it is and the is is a secure page, rewrite to ...

This can be done by looking at %{HTTP_HOST}:

RewriteCond %{HTTPS} !=on
RewriteRule /jquery\.min\.js$ https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js [NC,R=permanent]

RewriteCond %{HTTPS} =on
RewriteRule /jquery\.min\.js$ http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js [NC,R=permanent]

You should also escape the dots and teh RewriteCond you use is unnecessary; you can do it in the RewriteRule.

3.. Each individual domain will still have it's own custom mod-rewrite rules. Which rules take precedence - global or per-domain? Do they combine? Is it ok if I have the "RewriteEngine On" directive in the global httpd.conf and then again in the vhost.conf?

It will be prepended to the virtual host's mod_rewrite configuration.

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