4

我正在使用 .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]
重写 ^css/([^/]+)$ http://c.example.com/$1 [L]

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

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

背景:网址(/images/logo.jpg);不工作

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

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

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

如果我使用样式表工作:

背景:网址(http://i.example.com/logo.jpg);

但是,为了避免重写所有样式表,我想知道:为什么 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

4

2 回答 2

1

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

请求 domain.com/images/ 路由到 i.domain.com/images/
对 domain.com/css/ 的请求路由到 c.domain.com/css/

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

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

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

于 2009-03-11T12:50:31.980 回答
1

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

# 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/css/foo被重定向到http://c.example.com/foo并且样式表内部的图像 URI 引用/images/bar从这个新的基本 URI 解析并因此导致http://c.example.com/images/bar而不是初始的http://example.com/images/bar.

于 2009-03-11T12:52:53.067 回答