CakePHP 和 .htaccess 资源缓存

发布于 2024-12-11 09:08:20 字数 1947 浏览 0 评论 0原文

我对 CakePHP 还很陌生,并且无法弄清楚如何优化资源缓存。

当我仍然用纯 PHP 编码时,这就是我对 .htaccess 和 header.inc.php 文件所做的事情:

.htaccess:

<IfModule mod_rewrite.c>
    # Turn the rewrite engine on

    RewriteEngine On

    # The following rewrite rule makes it so that if a URL such as
    # http://example.com/css/style.1291314030.css is requested
    # then it will actually load the following URL instead (if it exists):
    #
    # http://example.com/css/style.css
    #
    # This is to increase the efficiency of caching. See http://bit.ly/9ZMVL for
    # more information.

    RewriteCond %{DOCUMENT_ROOT}/$1/$2.$3 -f
    RewriteRule ^(css|js)/(.*)\.[0-9]+\.(.*)$ /$1/$2.$3 [L]
</IfModule>

<IfModule mod_expires.c>
    # Optimize caching - see http://yhoo.it/ahEkX9 for more information.

    ExpiresActive On

    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 month"
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType application/x-javascript "access plus 1 year"
</IfModule>

header.inc.php:

foreach ($css_to_use as $current_css)
{
    echo "\n\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"css/$current_css." . filemtime("{$_SERVER['DOCUMENT_ROOT']}/css/$current_css.css") . ".css\">";
}

这个设置工作得很好,因为当我在客户端网站上工作时,我无需告诉客户端执行硬刷新或清除缓存;它是完全自动的,并且仍然具有缓存的优点。

我看到在 CakePHP 的“app/config/core.php”文件中,可以使用这一行代码:

Configure::write('Asset.timestamp', 'force');

但是,这只会使 URL 看起来像这样:

<link rel="stylesheet" type="text/css" href="/css/style.css?1291314030" />

所以它不能按照我希望的方式工作。实现资产缓存的最佳方式是什么?

谢谢!

I am still pretty new to CakePHP and am having trouble figuring out how to optimize asset caching.

Back when I still coded in pure PHP, this is what I would do with my .htaccess and header.inc.php files:

.htaccess:

<IfModule mod_rewrite.c>
    # Turn the rewrite engine on

    RewriteEngine On

    # The following rewrite rule makes it so that if a URL such as
    # http://example.com/css/style.1291314030.css is requested
    # then it will actually load the following URL instead (if it exists):
    #
    # http://example.com/css/style.css
    #
    # This is to increase the efficiency of caching. See http://bit.ly/9ZMVL for
    # more information.

    RewriteCond %{DOCUMENT_ROOT}/$1/$2.$3 -f
    RewriteRule ^(css|js)/(.*)\.[0-9]+\.(.*)$ /$1/$2.$3 [L]
</IfModule>

<IfModule mod_expires.c>
    # Optimize caching - see http://yhoo.it/ahEkX9 for more information.

    ExpiresActive On

    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 month"
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType application/x-javascript "access plus 1 year"
</IfModule>

header.inc.php:

foreach ($css_to_use as $current_css)
{
    echo "\n\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"css/$current_css." . filemtime("{$_SERVER['DOCUMENT_ROOT']}/css/$current_css.css") . ".css\">";
}

This setup worked quite well because when I worked on client websites, I never had to tell the client to perform a hard refresh or clear their cache; it was totally automatic and still had the benefits of caching.

I see that in CakePHP's "app/config/core.php" file, one can use this line of code:

Configure::write('Asset.timestamp', 'force');

However, that only makes the URLs look like this:

<link rel="stylesheet" type="text/css" href="/css/style.css?1291314030" />

So it doesn't work the way I'd like it to. What is the best way to achieve asset caching?

Thanks!

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

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

发布评论

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

评论(2

妄想挽回 2024-12-18 09:08:20

附加查询字符串实际上与更改 url 相同,浏览器会认为它不同并重新加载资源,无论是 CSS、图像还是其他任何内容。

Appending a query string is effectively the same as changing the url, browsers will consider it different and reload the asset, be it CSS, images or anything else.

幻想少年梦 2024-12-18 09:08:20

第 1 步:将您的 webroot .htacess 更改为此

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##

第 2 步:sudo a2enmod 过期

第 3 步:sudo service apache2 restart

第 4 步:喝杯啤酒,生活很美好。

Step 1: Change your webroot .htacess to this

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##

Step 2: sudo a2enmod expires

Step 3: sudo service apache2 restart

Step 4: Drink a beer, life is good.

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