2012-10-23 14 views
18

मैं अपनी वेबसाइट और पेज के लिए एक htaccess फ़ाइल बनाने की कोशिश कर रहा हूं। स्पीड अंतर्दृष्टि ने दिखाया है कि बिना छवियों के एक छवियां और एक सीएसएस फ़ाइल हैं।छवियों और सीएसएस के लिए htaccess लीवरेज ब्राउज़र कैशिंग

मुझे यकीन नहीं है कि इस से कहां से शुरू किया जाए या इसे कैसे किया जाए, मेरे पास यह कोड ऑनलाइन ट्यूटोरियल से है और यह सोच रहा था कि यह काम करने के लिए पर्याप्त होगा या नहीं।

<IfModule mod_expires.c> 
ExpiresActive On 
############################################ 
## Add default Expires header 
## http://developer.yahoo.com/performance/rules.html#expires 
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> 
ExpiresDefault "access plus 1 year" 
</FilesMatch> 
</IfModule> 

या क्या यह कोड करता है जो मुझे करने की ज़रूरत है?

धन्यवाद

उत्तर

37

तरह

<IfModule mod_expires.c> 
    ExpiresActive On 
    ExpiresDefault "access plus 1 seconds" 
    ExpiresByType text/html "access plus 1 seconds" 
    ExpiresByType image/x-icon "access plus 2592000 seconds" 
    ExpiresByType image/gif "access plus 2592000 seconds" 
    ExpiresByType image/jpeg "access plus 2592000 seconds" 
    ExpiresByType image/png "access plus 2592000 seconds" 
    ExpiresByType text/css "access plus 604800 seconds" 
    ExpiresByType text/javascript "access plus 86400 seconds" 
    ExpiresByType application/x-javascript "access plus 86400 seconds" 
</IfModule> 

या

<FilesMatch "\.(?i:gif|jpe?g|png|ico|css|js|swf)$"> 

    <IfModule mod_headers.c> 
    Header set Cache-Control "max-age=172800, public, must-revalidate" 
    </IfModule> 

</FilesMatch> 
+0

धन्यवाद, मैं इस में टैग के बीच के रूप में मैं इस कोशिश की और यह अभी भी समाप्ति के रूप में दिखाता सेट नहीं – Sam

+1

न इस में 'जोड़ने जोड़ना होगा ' आप इस जोड़ सकता है इस –

+0

से पहले या उसके बाद मैंने आपके द्वारा दिए गए कोड में जोड़ा है, जो कि समाप्त हो गया है 'एक्सपेयरएक्टिव ऑन एक्सपेयर्स बायटाइप एप्लिकेशन/एक्स-जावास्क्रिप्ट "एक्सेस प्लस 86400 सेकेंड"' मेरे पास .htaccess फ़ाइल में है और यह काम नहीं कर रहा है, कृपया आप सहायता। धन्यवाद – Sam

23

मैं जानता हूँ कि यह एक विलम्बित जवाब है कुछ की कोशिश है, लेकिन इसके बाद के संस्करण मेरे लिए काम नहीं किया। इसके बजाय मैं निम्नलिखित प्रयोग किया है:

<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|html|htm|xml|txt|xsl)$"> 
Header set Cache-Control "max-age=31536050" 
</FilesMatch> 
+1

यह मेरे लिए काम करता है और Google पेजपेड इसे अच्छे कारक के रूप में पहचानता है! – gleenk

+0

भी मेरे लिए काम किया, thx। वह अधिकतम उम्र कितनी लंबी है? – Marko

+0

@ मार्को यह एक साल है, सेकंड में। –

0
मैथ्यू जॉनसन के बारे में

"s जवाब ऊपर, के रूप में-है यह मेरे Laravel5.2 वेबसाइट पर मेरे लिए काम नहीं किया लेकिन मैं सूची में 'php' जोड़ा गया है और यह काम किया! ! आप मैथ्यू जॉनसन

<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|html|htm|xml|php|txt|xsl)$"> 
Header set Cache-Control "max-age=31536050" 
</FilesMatch> 
6

जवाब से ऊपर मेरे लिए काम कर रहा है, लेकिन धन्यवाद, मैं भी .htaccess में अन्य फ़ाइलों एक्सटेंशन जोड़ना चाहते हैं। कोड नीचे मुझे एक अच्छा समाधान दे।

पूर्ण .htaccess कोड गूगल पर पारित करने के लिए पेजस्पेड अंतर्दृष्टि:

  1. संपीड़न
  2. उत्तोलन ब्राउज़र कैशिंग
# Enable Compression 
<IfModule mod_deflate.c> 
    AddOutputFilterByType DEFLATE application/javascript 
    AddOutputFilterByType DEFLATE application/rss+xml 
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject 
    AddOutputFilterByType DEFLATE application/x-font 
    AddOutputFilterByType DEFLATE application/x-font-opentype 
    AddOutputFilterByType DEFLATE application/x-font-otf 
    AddOutputFilterByType DEFLATE application/x-font-truetype 
    AddOutputFilterByType DEFLATE application/x-font-ttf 
    AddOutputFilterByType DEFLATE application/x-javascript 
    AddOutputFilterByType DEFLATE application/xhtml+xml 
    AddOutputFilterByType DEFLATE application/xml 
    AddOutputFilterByType DEFLATE font/opentype 
    AddOutputFilterByType DEFLATE font/otf 
    AddOutputFilterByType DEFLATE font/ttf 
    AddOutputFilterByType DEFLATE image/svg+xml 
    AddOutputFilterByType DEFLATE image/x-icon 
    AddOutputFilterByType DEFLATE text/css 
    AddOutputFilterByType DEFLATE text/html 
    AddOutputFilterByType DEFLATE text/javascript 
    AddOutputFilterByType DEFLATE text/plain 
</IfModule> 
<IfModule mod_gzip.c> 
    mod_gzip_on Yes 
    mod_gzip_dechunk Yes 
    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$ 
    mod_gzip_item_include handler ^cgi-script$ 
    mod_gzip_item_include mime ^text/.* 
    mod_gzip_item_include mime ^application/x-javascript.* 
    mod_gzip_item_exclude mime ^image/.* 
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* 
</IfModule> 

# Leverage Browser Caching 
<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> 
<IfModule mod_headers.c> 
    <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$"> 
    Header set Cache-Control "max-age=2678400, public" 
    </filesmatch> 
    <filesmatch "\.(html|htm)$"> 
    Header set Cache-Control "max-age=7200, private, must-revalidate" 
    </filesmatch> 
    <filesmatch "\.(pdf)$"> 
    Header set Cache-Control "max-age=86400, public" 
    </filesmatch> 
    <filesmatch "\.(js)$"> 
    Header set Cache-Control "max-age=2678400, private" 
    </filesmatch> 
</IfModule> 

वहाँ भी कुछ विन्यास है विभिन्न वेब सर्वर के लिए here देखना सक्षम करें।
उम्मीद है कि यह 100/100 स्कोर प्राप्त करने में मदद करेगा।