2010-11-10 6 views
31

Ho recentemente analizzato il mio sito Web con addp di pagine su firebug. Mi ha suggerito di impostare la scadenza su CSS, JS e file immagine.Come faccio a impostare la scadenza su CSS, JS e Immagini

Mi chiedo come faccio?

+1

Dipende molto dalle piattaforme in uso: IIS6/7, Apache, ecc. – jordanbtucker

+0

Sto utilizzando Apache come server web. Sto visualizzando questi file tramite i miei file php o all'interno di file php – KoolKabin

risposta

40

Questo è quello che uso per fissare la stessa cosa quando ho eseguito il Page Speed ​​Addon:

<FilesMatch "\.(jpg|jpeg|png|gif|swf)$"> 
    Header set Cache-Control "max-age=604800, public" 
</FilesMatch> 

Questo va nel file .htaccess.

leggere su questa pagina per maggiori informazioni su come impostare la cache per i tipi di file e/o modificare la durata della cache:

http://www.askapache.com/htaccess/apache-speed-cache-control.html

+3

Ho ricevuto l'errore del server interno 500. Penso che il mio apache header_module non sia abilitato. ho altro modo? – KoolKabin

+4

hai ricevuto un errore 500 e poi hai accettato la risposta? –

+0

grazie amico anche per me anche dopo 3 anni –

0

Quello che faccio è quello di creare un file "expires.conf "e includerlo nella configurazione del file del sito di Apache. Puoi includere in .htaccess se vuoi. mio scade:

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

è necessario attivare il modulo scade nel apache.

-2

Meglio uno (che si trova a http://www.paulund.co.uk/set-expire-headers-in-htaccess, ma come 0 seconds non ha funzionato, ho cambiato in 1 seconds)

# These are pretty far-future expires headers 
# They assume you control versioning with cachebusting query params like: <script src="application.js?20100608"> 
# Additionally, consider that outdated proxies may miscache 
# 
# www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/ 
# If you don`t use filenames to version, lower the css and js to something like "access plus 1 week" 

<IfModule mod_expires.c> 
    ExpiresActive on 

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

# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5) 
    ExpiresByType text/cache-manifest  "access plus 1 seconds" 


# Your document html 
    ExpiresByType text/html     "access plus 1 seconds" 

# Data 
    ExpiresByType text/xml     "access plus 1 seconds" 
    ExpiresByType application/xml   "access plus 1 seconds" 
    ExpiresByType application/json   "access plus 1 seconds" 

# RSS feed 
    ExpiresByType application/rss+xml  "access plus 1 hour" 

# Favicon (cannot be renamed) 
    ExpiresByType image/x-icon    "access plus 1 week" 

# Media: images, video, audio 
    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 video/ogg     "access plus 1 month" 
    ExpiresByType audio/ogg     "access plus 1 month" 
    ExpiresByType video/mp4     "access plus 1 month" 
    ExpiresByType video/webm    "access plus 1 month" 

# HTC files (css3pie) 
    ExpiresByType text/x-component   "access plus 1 month" 

# Webfonts 
    ExpiresByType font/truetype    "access plus 1 month" 
    ExpiresByType font/opentype    "access plus 1 month" 
    ExpiresByType application/x-font-woff "access plus 1 month" 
    ExpiresByType image/svg+xml    "access plus 1 month" 
    ExpiresByType application/vnd.ms-fontobject "access plus 1 month" 

# CSS and JavaScript 
    ExpiresByType text/css     "access plus 1 seconds" 
    ExpiresByType application/javascript "access plus 1 month" 
    ExpiresByType text/javascript   "access plus 1 month" 

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

</IfModule> 
5

vorrei aggiungere questa soluzione per coloro che cercano di esso .. ..

sua funziona alla grande anche ... usando .htaccess

https://webmasters.stackexchange.com/a/5275/37765

<FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$"> 
    ExpiresActive On 
    ExpiresDefault A2592000 
</FilesMatch> 
+0

Grazie mille dare questa soluzione ..... –

+0

Ha funzionato per me. Tuttavia, voglio sapere cosa succede se un sito si rompe dopo aver aggiunto questo codice? – Rahul

Problemi correlati