» » » » Pagespeed için Tarayıcı Önbelleğe Alma (.htaccess)

Pagespeed için Tarayıcı Önbelleğe Alma (.htaccess)

mehmetkalyci
mehmetkalyci 18-02-2017, 16:43
Google Pagespeed'de tarayıcı önbelleğe alma sorununuz varsa kullanabileceğiniz bir yöntem. Dle.net.tr forumunda ve başka forumlarda bilimum kod denememe rağmen Google PageSpeed halen "tarayıcı önbelleğe almayı kullanın" hatası veriyordu. Aşağıdaki kodları denediğimde bu sorun ortadan kalktı ve pagespeed skorum 61'den 80'e, pagespeed insight skorum ise 80'den 97'ye çıktı. Bilmeyenler için kısaca açıklayayım. Belli bir süre boyunca sitenizi önbelleğe alıyor ve sayfa yenilendiğinde dosyaları tekrar tekrar yüklenmek zorunda kalmıyor. Mantık olarak kodlarda yazan süre dolduğunda dosyalar yenileniyor. Bu süre zarfında eğer dosya isimleri değişirse veya ziyaretçileriniz tarayıcı çerezlerini silerse dosyalar son haliyle önbelleğe tekrar alınıyor. Çok ziyaretçisi olan siteler için yararlı olacağını düşünüyorum. Kkodun orjinalinde .svg sıkıştırması yoktu, dle'de svg kullanıldığı için onu da ekledim.

.htaccess dosyanızda "RewriteEngine On" un üstüne ekleyin.
# Expires Headers - 2678400s = 31 days 
<ifmodule mod_expires.c> 
  ExpiresActive On 
  ExpiresDefault "access plus 2678400 seconds" 
  ExpiresByType text/html "access plus 7200 seconds" 
  ExpiresByType image/gif "access plus 2678400 seconds" 
  ExpiresByType image/jpeg "access plus 2678400 seconds" 
  ExpiresByType image/png "access plus 2678400 seconds" 
  ExpiresByType text/css "access plus 750000 seconds" 
  ExpiresByType text/javascript "access plus 2678400 seconds" 
  ExpiresByType application/x-javascript "access plus 2678400 seconds"
  ExpiresByType image/svg+xml "access plus 2678400 seconds"
</ifmodule> 
  
# Cache Headers 
<ifmodule mod_headers.c> 
  # Cache specified files for 31 days 
  <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$"> 
  Header set Cache-Control "max-age=2678400, public" 
  </filesmatch> 
  # Cache HTML files for a couple hours 
  <filesmatch "\.(html|htm)$"> 
  Header set Cache-Control "max-age=7200, private, must-revalidate" 
  </filesmatch> 
  # Cache PDFs for a day 
  <filesmatch "\.(pdf)$"> 
  Header set Cache-Control "max-age=86400, public" 
  </filesmatch> 
  # Cache Javascripts for 31 days 
  <filesmatch "\.(js)$"> 
  Header set Cache-Control "max-age=2678400, private" 
  </filesmatch> 
</ifmodule> 
Bilgilendirme
Yorum Ekleyebilmeniz için Sitemize Kayıt Olmanız Gerekmektedir.
0
Evan 18 Ocak 2018 16:48
Çok faydalı bir paylaşım. Elinize sağlık. Ben de benzer olarak şunu buldum:

<IfModule mod_expires.c>
	ExpiresActive on

	ExpiresDefault "access plus 1 month"

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

	# html
	ExpiresByType text/html "access plus 0 seconds"

	# XML
	ExpiresByType text/xml "access plus 0 seconds"
	ExpiresByType application/xml "access plus 0 seconds"

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

	# Favicon
	ExpiresByType image/x-icon "access plus 1 week"

	# Resimler
	ExpiresByType image/gif "access plus 1 month"
	ExpiresByType image/png "access plus 1 month"
	ExpiresByType image/jpeg "access plus 1 month"
	ExpiresByType image/jpg "access plus 1 month"

	# HTC dosyaları (örneğin css3pie)
	ExpiresByType text/x-component "access plus 1 month"

	# Standart dışı site fonksiyonları
	ExpiresByType application/x-font-ttf "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 ve javascript
	ExpiresByType text/css "access plus 1 year"
	ExpiresByType application/javascript "access plus 1 year"

</IfModule>

# Cache-Control tarayıcı 
<ifModule mod_headers.c>
	# 30den fazla
	<filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
	Header set Cache-Control "max-age=2592000, public"
	</filesMatch>
	# 30 gün
	<filesMatch ".(css|js)$">
	Header set Cache-Control "max-age=2592000, public"
	</filesMatch>
	# 2 gün
	<filesMatch ".(xml|txt)$">
	Header set Cache-Control "max-age=172800, public, must-revalidate"
	</filesMatch>
	# 1 день
	<filesMatch ".(html|htm|php)$">
	Header set Cache-Control "max-age=172800, private, must-revalidate"
	</filesMatch>
</ifModule>

<IfModule mod_setenvif.c>
	#Эта конструкция для говнобраузера
	#Запрет отдачи HTTP-заголовков Vary
	BrowserMatch "MSIE" force-no-vary
	BrowserMatch "Mozilla/4.[0-9]{2}" force-no-vary
</IfModule>
0
Reborn 29 Mart 2017 20:39
Aradığım bir çözümdü. Google'nin webmaster araçlar kısmında site optimizasyonu için bunu öneriyor.
deneme bonusu veren siteler