利用浏览器缓存.htaccess无法正常工作

时间:2016-12-06 13:28:22

标签: php apache .htaccess mod-rewrite caching

我正在使用laravel 5,速度我在谷歌速度洞察中测试,它显示杠杆浏览器缓存

解决我已经使用了以下.htaccess代码,但没有使用它们

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^(.*)$ public/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
</IfModule>

<IfModule mod_headers.c>
# Serve gzip compressed CSS files if they exist 
# and the client accepts gzip.
RewriteCond "%{HTTP:Accept-encoding}" "gzip"
RewriteCond "%{REQUEST_FILENAME}\.gz" -s
RewriteRule "^(.*)\.css" "$1\.css\.gz" [QSA]

# Serve gzip compressed JS files if they exist 
# and the client accepts gzip.
RewriteCond "%{HTTP:Accept-encoding}" "gzip"
RewriteCond "%{REQUEST_FILENAME}\.gz" -s
RewriteRule "^(.*)\.js" "$1\.js\.gz" [QSA]


# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-gzip:1]
RewriteRule "\.js\.gz$" "-" [T=text/javascript,E=no-gzip:1]


<FilesMatch "(\.js\.gz|\.css\.gz)$">
  # Serve correct encoding type.
  Header append Content-Encoding gzip

  # Force proxies to cache gzipped & 
  # non-gzipped css/js files separately.
  Header append Vary Accept-Encoding
</FilesMatch>
</IfModule>

<IfModule mod_expires.c>
ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault "access plus 1 month"

# Media: images, video, audio
ExpiresByType assets/images "access plus 1 month" 

# CSS and JavaScript
ExpiresByType assets/css "access plus 1 month"
ExpiresByType assets/js "access plus 1 month"

<IfModule mod_headers.c>
 Header append Cache-Control "public"
</IfModule>

</IfModule>

我在.htaccess中使用的第二个代码就是这个

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^(.*)$ public/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 year"
ExpiresByType text/html "access plus 1 second"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 604800 seconds"
ExpiresByType application/x-javascript "access plus 604800 seconds"
</IfModule>

我也尝试过.htaccess中的这个代码使它工作,但是这个也没有用

<IfModule mod_expires.c>
# Set Cache-Control and Expires headers
<filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "max-age=2592000, public"
Header set Expires "1 day"
</filesMatch>
<filesMatch "\\.(css|css.gz)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\\.(js|js.gz)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\\.(xml|txt)$">
Header set Cache-Control "max-age=216000, public, must-revalidate"
</filesMatch>
<filesMatch "\\.(html|htm)$">
Header set Cache-Control "max-age=7200, public, must-revalidate"
</filesMatch>
</IfModule>

我已经使用了以上所有代码,但每次都得到相同的结果 应修复:利用浏览器缓存

在大多数anaylser上。

是我在

中缺少的任何内容

2 个答案:

答案 0 :(得分:2)

如果你在ubuntu中试试这个

sudo a2enmod expires
sudo systemctl restart apache2

答案 1 :(得分:1)

检查服务器中是否启用了模块。

如果未启用模块,则代码将无效。