Thứ Bảy, 23 tháng 4, 2016

22:01:00
Để tăng tốc độ cho 1 website thì bạn cần hội tụ đủ yếu tố tối ưu code,nhưng ko phải ai cũng làm được,nhưng chúng ta có thể sử dụng một số thủ thuật nhỏ khiến website bạn có tốc độ tốt nhất (hiện tại webchoban cũng đang dùng code này )

[accordion state=”closed” color=”gray” title=”.htaccess” tag=”h3″ ]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} -fRewriteRule ^(.*)(js|css)$ redir.php?file=$1$2&type=$2 [L] </IfModule>####ETagsFileETag None####Expires<IfModule mod_expires.c>ExpiresActive OnExpiresByType image/gif A2592000ExpiresByType image/jpeg A2592000ExpiresByType image/png A2592000ExpiresByType application/x-shockwave-flash A2592000ExpiresByType text/css A2592000ExpiresByType application/x-javascript A2592000 [/accordion] [accordion state=”closed” color=”gray” title=”pre.php (với nội dung)” tag=”h3″ ]<?php $path = pathinfo($_SERVER[‘SCRIPT_NAME’]);if ($path[‘extension’] == ’css’)  {header(‘Content-type: text/css’);}if ($path[‘extension’] == ’js’)  {header(‘Content-type: application/x-javascript’);}?> [/accordion][accordion state=”closed” color=”gray” title=”redir.php” tag=”h3″ ] <?php # this is the file redir.php, to gzip javascript and css # set the request file name$file=str_replace(chr(0×0),””,$_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”);} # 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();}?> [/accordion] 

Để chạy pre.php ,ta cần 1 tập tin hệ thống là php.ini hoạt động ngầm, thực thi pre.php

php.ini đặt tại root, nội dung sau:

zlib.output_compression = on

zlib.output_compression_level = 3
auto_prepend_file = ”pre.php”


Mục đích tạo php.ini nhằm nén tập tin ở lớp PHP handler (.php) giúp bạn hệ thống nhanh hơn, khi vừa được nén các file xử lý php, nén các tập tin khi kết hợp nén module deflate.

nguồn: sưu tầm

0 Bình luận:

Đăng nhận xét

Khi các bạn đóng góp ý kiến nhớ chú ý ngôn từ cho phù hợp với thuần phong ,mỹ tục văn hóa nước ta. Cảm ơn!