google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Tải các tệp JavaScript & CSS nhanh hơn với PHP mod_rewrite Hiện tại, thế giới Internet đã thay đổi, trở nên quan trọng hơn và nhiều giá trị nội dung hơn, giao diện các website trở nên đẹp và tiện dụng hơn và hiển nhiên các trang web cũng cần nhiều thời gian hơn để tải về. Nhưng hầu hết các khách truy cập đều không muốn sự chờ đợi này, đây cũng là vấn đề đau đầu với tất cả các nhà phát triển web. Bài viết này hướng dẫn bạn một trong nhiều cách để rút ngắn thời gian tải trang web, đó là thiết lập thời hạn của các tệp tin CSS và JavaScript, đồng thời nén mã nguồn JavaScript, CSS lại trước khi trả về trình duyệt.

Vui lòng vào trang chi tiết để xem hướng dẫn đầy đủ và xem thêm các bài viết tối ưu khác:

- Các thủ thuật JavaScript và jQuery hữu ích
- Vài giải pháp cơ bản để tăng tốc JavaScript
- Vài hướng dẫn JavaScript cơ bản để tối ưu hóa hiệu suất trang web


Nhãn: tải các tệp, PHP, mod_rewrite, nhà phát triển web, rút ngắn thời gian, thời hạn, nén mã nguồn

Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

The landscape of the internet has changed a great deal over the years. Websites are no longer static and lifeless, they have become dynamic and interactive. A consequence of this is that the size of modern websites has increased significantly. This poses a couple of important issues:

  1. It takes more time for webpages to be downloaded by client browsers.
  2. Average website bandwidth usage has increased.

Most internet surfers won't wait 30 seconds for a page to load in its entirety, they'll simply move on to something else. Also, if you own or manage an even moderately busy website you will know just how much bandwidth is worth. It is a precious commodity that can increase the figures on your monthly hosting bill. As a result of this, a lot of emphasis is now being placed on website optimization.

Yahoo has published a set of rules which outlines some of the methods that can be employed to make your website as lean and fast as possible. One of the methods suggested is to compress and minify external CSS and JavaScript files. Some PHP frameworks employ some form of optimization of these files and if you have control over your server you can always install mod_gzip which would handle compression of these files. If you are not using a PHP framework or you use a shared hosting plan (most of which won't allow you to use mod_gzip because it can be a CPU hog) then read on.

First off, we will create a .htaccess file in which we will put our rewrite rules:

IndexIgnore *

<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteRule ^(.*\.((js)|(css)))$ optimizer.php?file=$1
</IfModule>

Place a copy of this file in your /js and your /css folders. The first line really has nothing to with our optimization, all it does is it prevents listing of the contents of the directory when someone navigates to http://www.yourdomain.com/js or http://www.yourdomain.com/css. Not necessary but good practice. The next few lines redirects incoming requests to the file optimizer.php if the file extension is .js or .css and passes the filename to it as a query string. Next, we'll create a file called optimizer.php:

<?php
if(!isset($_GET['file'])) die('Invalid parameter!');

$file = file_get_contents($_GET['file']);
$file = preg_replace("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/", "", $file);

header("Expires: Fri, 21 Dec 2012 00:00:00 GMT");
header("Content-type: application/x-javascript");
ob_start("ob_gzhandler");

echo $file;
ob_flush();
flush();
?>

This file should also be placed in both the /css and /js folders. What this script does is it grabs the filename from the $_GET array, reads the contents of the file into a variable, strips the file of all comments, sends content type and expires headers to the browser, compresses the contents of the variable (via output buffering) and sends the optimized CSS or JavaScript back to the browser. Simple and straightforward.

This method is a simple way to optimize your external .js and .css files when you don't have access to better(?) methods. The only issue I have noted so far with this method is that it will mess up the minified version of the jQuery library which really isn't an issue since its already minified. If you are using the minified version of jQuery on your website simply use this code in your optimize.php file instead:

<?php
if(!isset($_GET['file'])) die('Invalid parameter!');

$file = file_get_contents($_GET['file']);
$jqueryFile = 'name_of_your-jquery_file';

if($file!=$jqueryFile) $file = preg_replace("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/", "", $file);

header("Expires: Fri, 21 Dec 2012 00:00:00 GMT");
header("Content-type: application/x-javascript");
ob_start("ob_gzhandler");

echo $file;
ob_flush();
flush();
?>

Now go optimize your stylesheets and JavaScripts and make your users happier!

Footnote #1: This implementation will obviously only work on an apache server as a .htaccess file is used.

Footnote #2: The code used in this post is simply a proof of concept. Little or no attention was paid to security. In an actual implementation of this technique you would need to ensure that this script is as secure as possible.

iPhoneKer.com
Save up to 630$ when buy new iPhone 15

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

Dall-E-OpenAI.com
Generate creative images automatically with AI

AIVideo-App.com
Render creative video automatically with AI

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web