通过 mod_rewrite 网站速度提高了 5 倍,但 CSS 图像损坏

发布于 2024-07-15 01:39:30 字数 2474 浏览 2 评论 0原文

我正在使用 .htaccess 通过以下重定向来加速网站:

request for http://example.com/images/name.jpg  routed to  http://i.example.com/name.jpg
request for http://example.com/css/name.css     routed to  http://c.example.com/name.css

通过收听 Stack Overflow 播客,我了解到这可以使网站速度更快,因为浏览器可以同时下载更多文件(显然是两个每个域的流,尽管这尚未得到证实)。

事实上,差异是巨大的。 页面加载速度大约五倍

我没有触及原始文件夹和图像 - 我只是使用 mod_rewrite 将地址从 example.com/images/ 更改为 i.example.com/

rewritecond %{HTTP_HOST}     !^i\.example\.com       [NC]
rewriterule ^images/([^/]+)$ http://i.example.com/$1 [L]
rewritecond %{HTTP_HOST}     !^c\.example\.com       [NC]
rewriterule ^css/([^/]+)$    http://c.example.com/$1 [L]

:我遇到的问题是,这种技术非常适合 html 中包含的图像标签,但不适用于通过样式表包含的图像:

img src=/images/logo.jpg 完美运行

背景:url(/images/logo.jpg);不起作用

服务器错误日志包含以下条目:

文件不存在:/var/www/html/css/images,引用地址:http://example .com/page.html

这似乎意味着重写规则应用不正确。

如果我使用以下样式表,则样式表可以工作:

背景:url(http://i.example.com/logo.jpg< /a>);

但是,为了避免重写所有样式表,我想知道:为什么 url 重写不像应用于 html img 标签那样应用于样式表

[update1]此问题存在于 Safari 4 Beta、Firefox 3.0.3 和 Chrome 中,但页面在 IE6 中完美运行

[update2] 添加 [L,R=301] 和 [L,R=302] 没有帮助。

[update3] 我根据下面 Gumbo 的建议尝试了以下操作:

如果路径与主机名不匹配,则从外部重定向:

rewritecond %{HTTP_HOST}     !^i\.domain\.com$
rewriterule ^images/([^/]+)$ http://i.domain.com/$1 [L,R=301]
rewritecond %{HTTP_HOST}     !^c\.domain\.com$
rewriterule ^css/([^/]+)$    http://c.domain.com/$1 [L,R=301]

内部重定向; 如果有不必要的文件夹名称,请将其删除(请参阅上面的服务器错误):

rewritecond %{HTTP_HOST}     ^i\.domain\.com$
rewriterule ^images/([^/]+)$ $1 [L]
rewritecond %{HTTP_HOST}     ^c\.domain\.com$
rewriterule ^css/([^/]+)$    $1 [L]

它仍然不起作用。 奇怪的是,服务器错误是:

文件不存在:/var/www/html/css/var,引用地址:http://domain .com/page.html

I am using .htaccess to accelerate a site with the following redirects:

request for http://example.com/images/name.jpg  routed to  http://i.example.com/name.jpg
request for http://example.com/css/name.css     routed to  http://c.example.com/name.css

From listening to the Stack Overflow podcast, I learned that this could make a site faster, since the browser can download more files simultaneously (apparently two streams per domain, although this is unconfirmed).

Indeed, the difference is dramatic; the page loads about five times as fast!

I have not touched the original folders and images -- I am just using mod_rewrite to change the addresses from example.com/images/ to i.example.com/:

rewritecond %{HTTP_HOST}     !^i\.example\.com       [NC]
rewriterule ^images/([^/]+)$ http://i.example.com/$1 [L]
rewritecond %{HTTP_HOST}     !^c\.example\.com       [NC]
rewriterule ^css/([^/]+)$    http://c.example.com/$1 [L]

The problem I have is that this technique works perfectly for image tags included in html, but doesn't work for images included via stylesheets:

img src=/images/logo.jpg works perfectly

background:url(/images/logo.jpg); does not work

The server error log contains the following entry:

File does not exist: /var/www/html/css/images, referer: http://example.com/page.html

This seems to imply that the rewrite rule is being applied incorrectly.

The stylesheets work if I use:

background:url(http://i.example.com/logo.jpg);

However, in order to avoid rewriting all the style sheets, I'd like to know: why doesn't url rewriting apply to stylesheets the way it does to html img tags.

[update1] This problem exists in Safari 4 Beta, Firefox 3.0.3, and Chrome, but the page works perfectly in IE6.

[update2] Adding [L,R=301] and [L,R=302] did not help.

[update3] I tried the following based on Gumbo's suggestion below:

Redirect externally if path doesn’t match host name:

rewritecond %{HTTP_HOST}     !^i\.domain\.com$
rewriterule ^images/([^/]+)$ http://i.domain.com/$1 [L,R=301]
rewritecond %{HTTP_HOST}     !^c\.domain\.com$
rewriterule ^css/([^/]+)$    http://c.domain.com/$1 [L,R=301]

Redirect internally; if there's an unnecessary folder name remove it (see server error above):

rewritecond %{HTTP_HOST}     ^i\.domain\.com$
rewriterule ^images/([^/]+)$ $1 [L]
rewritecond %{HTTP_HOST}     ^c\.domain\.com$
rewriterule ^css/([^/]+)$    $1 [L]

It still didn't work. Bizarrely, the server error is:

File does not exist: /var/www/html/css/var, referer: http://domain.com/page.html

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

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

发布评论

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

评论(2

夜灵血窟げ 2024-07-22 01:39:31

我能够通过不尝试将目录合并到子域来解决这个问题:

request for domain.com/images/  routed to  i.domain.com/images/
request for domain.com/css/     routed to  c.domain.com/css/

它工作完美并且仍然非常快

现代浏览器中似乎存在一个错误,其中重定向的 css 请求将仅应用新域,而将原始目录保留为请求的一部分:

如果 css url(domain.com/images/name.jpg)处的图片被重定向到i.domain.com/name.jpg,浏览器会错误地请求i .domain.com/images/name.jpg

I was able to resolve this by not trying to incorporate directories into the subdomains:

request for domain.com/images/  routed to  i.domain.com/images/
request for domain.com/css/     routed to  c.domain.com/css/

It works perfectly and is still extremely fast.

There seems to be a bug in modern browsers where a css request that is redirected will apply only the new domain, leaving the original directories as part of the request:

If a css image at url(domain.com/images/name.jpg) is redirected to i.domain.com/name.jpg, the browser will mistakenly request i.domain.com/images/name.jpg.

别把无礼当个性 2024-07-22 01:39:31

如果所有主机名都使用相同的虚拟主机,我找到了解决此问题的方法:

# redirect externally if path doesn’t match host name
RewriteCond %{HTTP_HOST} !^i\.example\.com$
RewriteRule ^images/([^/]+)$ http://i.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^c\.example\.com$
RewriteRule ^css/([^/]+)$ http://c.example.com/$1 [L,R=301]

# redirect internally to the file
RewriteCond %{HTTP_HOST} ^i\.example\.com$
RewriteRule !^images/ images%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^c\.example\.com$
RewriteRule !^css/ css%{REQUEST_URI} [L]

这将执行以下操作:

http://example.com/css/foo       externally to    http://c.example.com/foo
http://c.example.com/foo         internally to    /css/foo

http://example.com/images/bar    externally to    http://i.example.com/bar
http://i.example.com/bar         internally to    /images/bar

以及更正不匹配的路径和主机名:

http://i.example.com/css/foo     externally to    http://c.example.com/foo
http://c.example.com/images/bar  externally to    http://i.example.com/bar

当请求的样式表 http://example.com 时,会发生不匹配。 com/css/foo 被重定向到 http://c.example.com/foo 以及样式表内的图像 URI 引用,例如 /images/bar从这个新的基本 URI 解析,从而导致 http://c.example.com/images/bar 而不是最初的 http://example.com/images/bar< /代码>。

I found a way to solve this problem if all host names use the same virtual host:

# redirect externally if path doesn’t match host name
RewriteCond %{HTTP_HOST} !^i\.example\.com$
RewriteRule ^images/([^/]+)$ http://i.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^c\.example\.com$
RewriteRule ^css/([^/]+)$ http://c.example.com/$1 [L,R=301]

# redirect internally to the file
RewriteCond %{HTTP_HOST} ^i\.example\.com$
RewriteRule !^images/ images%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^c\.example\.com$
RewriteRule !^css/ css%{REQUEST_URI} [L]

This will do the following:

http://example.com/css/foo       externally to    http://c.example.com/foo
http://c.example.com/foo         internally to    /css/foo

http://example.com/images/bar    externally to    http://i.example.com/bar
http://i.example.com/bar         internally to    /images/bar

As well as correcting mismatching paths and host names:

http://i.example.com/css/foo     externally to    http://c.example.com/foo
http://c.example.com/images/bar  externally to    http://i.example.com/bar

A mismatch occurs when the requested stylesheet http://example.com/css/foo is redirected to http://c.example.com/foo and an image URI reference like /images/bar inside the stylesheet is resolved from this new base URI and thus leading to http://c.example.com/images/bar instead of the initial http://example.com/images/bar.

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