2016-12-06 75 views
1

我使用laravel 5,對速度測試我在谷歌的速度洞察力,其顯示槓桿瀏覽器緩存槓桿瀏覽器緩存的.htaccess不工作

解決,我已經使用下面的.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的。

是我在此丟失的任何東西

+0

它可以在你的HTML被期待緩存頭,但不會真正提高你的速度沒有服務預先生成的內容。另外,第一組和第三組規則期望您提供預壓縮的JS和CSS。 – Walf

回答

1

檢查模塊是否在服務器中啓用。

如果未啓用模塊,則代碼不起作用。

1

如果您是在Ubuntu試試這個

sudo a2enmod expires 
sudo systemctl restart apache2 
+0

感謝其幫助我:) – viji