Thiết lập Expires Header trong PHP & Apache. Có 2 cách để làm điều này, có thể chỉ định ở phần head trong code php, nó sẽ hữu ích khi bạn muốn điều chỉnh động thời gian hết hạn. Ví dụ với page không có sự thay đổi nhiều thì nên thiết lập thời gian sử dụng lâu dài:
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // 1 hour
Lựa chọn thứ 2 bạn tạo trong file .htaccess hoặc sửa ở file httpd. Trong môi trường shared hosting thì can thiệp vào file .htaccess là phổ biến. Để thực hiện yêu cầu server của bạn có hỗ trợ mod_expires, mod_headers hoặc cả hai. Đặt đoạn code sau vào file .htaccess
# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
# Set up caching on media files for 1 year (forever?)
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
</IfModule>
Nếu mod_expires chưa kích hoạt trên server, bạn có thể thử cách này:
<FilesMatch ".(ico|jpg|jpeg|png|gif|js|css)$">
Header set Expires "Sun, 22 Apr 2018 01:10:54 GMT"
Header set Cache-Control "max-age=315360000"
Header unset Pragma
</FilesMatch>
Keep alive:
Webpage thường xuyên thu thập nhiều files và nếu một kết nối mới được tạo sẽ dồn lại làm chậm hiển thị website khi keep alive chưa được thiết lập giai đoạn sử lý này có thể làm tăng thời gian tải nội dung website và gây lãng phí tài nguyên server.
Kiểm tra keep-alive có thiết lập trên website của bạn tại đây: http://www.giftofspeed.com/check-keep-alive
Cách 1: Sử dụng .htaccess:
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
Cách 2: kích hoạt keep-alive trong Apache.
Mở file cấu hình Apache, sửa lại với thông tin như sau:
#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On
#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 100
Tắt ETags.
Nó sẽ làm tăng chỉ số YSlow. Thêm dòng sau vào file .htaccess.
FileETag none
Cache Favicon.
Apache không hiểu kiểu file ICO, do đó chúng ta thêm dòng này vào .htaccess