Thứ Sáu, 5 tháng 10, 2012

Code tăng tốc load website php

Chào các bạn.

Mình viết bài viết này với mục tiêu chia sẻ code tăng tốc độ load nhằm tối ưu hóa website tốt hơn. Một trong những yếu tố góp phần google index nhanh website của bạn.

Về ý tưởng tăng tốc độ load website, mình xin mô tả là đoạn code này dựa trên yếu tố trình duyệt là chủ yếu. Nội dung website khi đã load 1 lần rồi, thì nó sẽ lưu lại dữ liệu là các tập tin javascript, hình ảnh (gif, png, jpg), css,... Ngoài việc lưu lại các tập tin này, thì nó sẽ còn gia tăng chỉ số expires của các tập tin, giúp nó lưu vào trình duyệt lâu hơn.

Bạn cần tạo 3 tập tin sau:

.htaccess (với nội dung):


PHP Code:
ExpiresActive on
ExpiresDefault 
"access plus 1 months"ExpiresByType image/jpg "access plus 1 months"ExpiresByType image/gif "access plus 1 months"ExpiresByType image/jpeg "access plus 1 months"ExpiresByType image/png "access plus 1 months"ExpiresByType text/css "access plus 1 months"ExpiresByType text/javascript "access plus 1 months"ExpiresByType application/javascript "access plus 1 months"ExpiresByType application/x-shockwave-flash "access plus 1 months"# Enable gzip (deflate) compressionAddOutputFilterByType DEFLATE text/html text/css application/x-javascript

RewriteEngine On
####CharsetAddDefaultCharset Off ####Gzip<IfModule mod_rewrite.c>RewriteCond %{REQUEST_FILENAME} -f
RewriteRule 
^(.*)(js|css)$ redir.php?file=$1$2&type=$[L]
</
IfModule>####ETagsFileETag None####Expires<IfModule mod_expires.c>ExpiresActive On
ExpiresByType image
/gif A2592000
ExpiresByType image
/jpeg A2592000
ExpiresByType image
/png A2592000
ExpiresByType application
/x-shockwave-flash A2592000
ExpiresByType text
/css A2592000
ExpiresByType application
/x-javascript A2592000  
pre.php (với nội dung):


PHP Code:
<?php # this is the file redir.php, to gzip javascript and css

# set the request file name
$file=str_replace(chr(0x0),"",$_REQUEST['file']);$allowedfiles = array('js','gif','png','jpg','css','txt','swf');
if (!
in_array(str_replace(chr(0x2E),"",substr(chr(0x2E).$file,-3)),$allowedfiles)){ exit ("Hacking attempt!"); }# Set Expires, cache the file on the browseheader("Expires:".gmdate("D, d M Y H:i:s"time()+15360000)."GMT");header("Cache-Control: max-age=315360000");# set the last modified time$mtime filemtime($file);$gmt_mtime gmdate('D, d M Y H:i:s'$mtime) . ' GMT';header("Last-Modified:" $gmt_mtime);# output a mediatype headerswitch ($_REQUEST['type']){
  case 
'css':
    
header("Content-type: text/css");
    break;
  case 
'js' :
    
header("Content-type: text/javascript");
      break;
  default:
    
header("Content-type: text/plain");
}
# GZIP the contentif(extension_loaded('zlib')){ob_start();ob_start('ob_gzhandler');}# echo the file's contentsecho implode(''file($file));

if(
extension_loaded('zlib')){
  
ob_end_flush();
  
# set header the content's length;
  # header("Content-Length: ".ob_get_length()); # (It doesn't work? )
  
ob_end_flush();
}
?>
Hãy thử và cảm nhận tốc độ load website của bạn nhé. 

Nguồn tin : IDVS

Đăng nhận xét

Credits