wordpress .htaccess、YSlow 和“使用无 cookie 域”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1433131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.htaccess, YSlow, and "Use cookie-free domains"
提问by Jason Pearce
One of YSlow's measurables is to use cookie-free domains to serve static files.
YSlow 的衡量标准之一是使用无 cookie 的域来提供静态文件。
"When the browser requests a static image and sends cookies with the request, the server ignores the cookies. These cookies are unnecessary network traffic. To workaround this problem, make sure that static components are requested with cookie-free requests by creating a subdomain and hosting them there." -- Yahoo YSlow
“当浏览器请求静态图像并随请求发送 cookie 时,服务器会忽略 cookie。这些 cookie 是不必要的网络流量。要解决此问题,请确保通过创建子域和无 cookie 请求来请求静态组件并在那里接待他们。” ——雅虎YSlow
I interpret this to mean that I could experience performance gains if I move www.example.com/imagesto static.example.com/images.
我认为这意味着如果我将www.example.com/images移动到static.example.com/images,我可以获得性能提升。
Although this is easy to do, I would lose the handy ability within my content management system (Joomla/WordPress) to easily reference and link to these images.
虽然这很容易做到,但我将失去在我的内容管理系统 (Joomla/WordPress) 中轻松引用和链接到这些图像的便利能力。
Is it possible to use .htaccess to redirect all requests for a particular folder on www.example.comto a folder on static.example.cominstead? Would this method also fool the CMS into thinking the images were located in the default locations on its own domain?
是否有可能使用的.htaccess来对特定文件夹的所有请求重定向www.example.com到一个文件夹上static.example.com呢?这种方法是否还会让 CMS 认为图像位于其自己域的默认位置?
采纳答案by Quentin
Is it possible to use .htaccess to redirect all requests for a particular folder on www.example.com to a folder on static.example.com instead?
是否可以使用 .htaccess 将 www.example.com 上特定文件夹的所有请求重定向到 static.example.com 上的文件夹?
Possible, but counter productive — the client would have to make an HTTP request, get the redirect response, then make another HTTP request.
可能,但适得其反——客户端必须发出一个 HTTP 请求,获得重定向响应,然后发出另一个 HTTP 请求。
This costs a lot more than the single line of cookie data saved!
这比保存一行 cookie 数据的成本要高得多!
Would this method also fool the CMS into thinking the images were located in the default locations on its own domain?
这种方法是否还会让 CMS 认为图像位于其自己域的默认位置?
No.
不。
回答by GmonC
Although this is easy to do, I would lose the handy ability within my content management system (Joomla/WordPress) to easily reference and link to these images.
虽然这很容易做到,但我将失去在我的内容管理系统 (Joomla/WordPress) 中轻松引用和链接到这些图像的便利能力。
What you could try to do is create a plugin in Joomla that dinamically creates these references.
您可以尝试在 Joomla 中创建一个插件,以动态创建这些引用。
For example, you have a plugin that when you enter {dinamic_path path} in an article, it appends 'static.example.com/images' to the path provided. So, everytime you need to change the server path, you just change in the plugin. For the links that are already in the database, you can try to use phpMyAdmin to change them in this structure.
例如,您有一个插件,当您在文章中输入 {dinamic_path path} 时,它会将“static.example.com/images”附加到所提供的路径。因此,每次您需要更改服务器路径时,您只需更改插件即可。对于已经在数据库中的链接,你可以尝试使用phpMyAdmin来改变它们在这个结构中。
It still loses the WYSIWYG hability in TinyMCE, but is an alternative.
它仍然失去了 TinyMCE 中的 WYSIWYG 功能,但它是一个替代方案。
回答by Martin Anderson-Clutz
In theory you could create a virtual domain that points directly to the images folder, such as images.example.com. Then in your CMS (hopefully at the theme layer) you could replace any paths that point to the images folder with an absolute path to the subdomain.
理论上,您可以创建一个直接指向图像文件夹的虚拟域,例如 images.example.com。然后在您的 CMS(希望在主题层)中,您可以将指向图像文件夹的任何路径替换为子域的绝对路径。
回答by RichieHindle
The redirects would cause far more network traffic, and farmore latency, than simply leaving things as they are.
重定向将导致远远更多的网络流量,并远远多的等待时间,不是简单地把一切事情,因为它们。
回答by Ryan Kearney
It would redirect the request but the client would still be sending its cookies to the server, so really you accomplished nothing. You would have to directly access the files from a domain that isn't storing cookies for it to work.
它会重定向请求,但客户端仍会将其 cookie 发送到服务器,因此您实际上什么也没做。您必须直接从未存储 cookie 的域中访问文件才能使其工作。
回答by Micha? W
I did below and gained success:
我在下面做了并获得了成功:
<FilesMatch "!\.(gif|jpe?g|png)$">
php_value session.cookie_domain example.com
</FilesMatch>
What it means is that if you do not set images in cookie information. Then images are cookie-free with server.
它的意思是如果你没有在cookie信息中设置图像。然后图像在服务器上是无 cookie 的。
回答by Micha? W
What you really want to do is use staticexample.com/images instead of static.example.com/images so that you don't pick up any cookies on the example.com domain that you may have set. If all you do is server images from that domain with a simple apache server or something then you can configure that server not to return even a session cookie.
您真正想要做的是使用 staticexample.com/images 而不是 static.example.com/images,这样您就不会在您可能设置的 example.com 域上获取任何 cookie。如果您所做的只是使用简单的 apache 服务器或其他东西从该域中获取服务器图像,那么您可以将该服务器配置为不返回会话 cookie。
The redirects are a very bad idea. Cookies cause some performance hits but round trips to the server such as a redirect would cause are a much more serious performance issue.
重定向是一个非常糟糕的主意。Cookie 会导致一些性能下降,但到服务器的往返行程(例如重定向)会导致更严重的性能问题。