Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过mod_重写,站点速度提高5倍,但CSS图像被破坏_Css_.htaccess_Mod Rewrite_Image_Subdomain - Fatal编程技术网

通过mod_重写,站点速度提高5倍,但CSS图像被破坏

通过mod_重写,站点速度提高5倍,但CSS图像被破坏,css,.htaccess,mod-rewrite,image,subdomain,Css,.htaccess,Mod Rewrite,Image,Subdomain,我使用.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播客,我了解到这可以使网站更快,因为浏览器可以同时下载更

我使用.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] request for domain.com/images/ routed to i.domain.com/images/ request for domain.com/css/ routed to c.domain.com/css/ 内部重定向;如果有不必要的文件夹名称,请将其删除(请参阅上面的服务器错误):

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

文件不存在:/var/www/html/css/var,请参考:


我可以通过不尝试将目录合并到子域中来解决这个问题

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] request for domain.com/images/ routed to i.domain.com/images/ request for domain.com/css/ routed to c.domain.com/css/ 请求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

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

# 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

您真的在样式表中使用绝对路径吗?我对这种行为的唯一解释是,样式表“/css/bar”中的相对路径“images/foo”已(正确)解析为“/css/images/foo”。使用绝对路径可以解决这个问题。我回去验证了,它们确实是绝对路径。由于不同浏览器中的行为会发生变化,这可能与浏览器出于缓存原因试图解释CSS图像的方式有关。我还尝试了../images和.././images,但仍然失败。除此之外,您的方法将导致302外部重定向,从而使请求数增加一倍。你确定要这个吗?为什么不首先使用不同的域?可能样式表认为它位于c.example.com,并在c.example.com/images/logo.jpg上查找URL?此解决方案在内部重新映射到/images/bar,而不是我需要的。我认为解决最初问题的方法是重新映射到。它不起作用(见修订后的问题)。