TODO: To change the header's content go to Dr.Explain menu Options : Project Settings : HTML (CHM) Export : Setup HTML Template and Layout
×
Menu
Index

Tối ưu với htaccess

 
GZip Compression.
Trong cms/framework khác để kích hoạt Gzip bạn sử dụng đoạn code sau chèn vào file .htaccess
<IfModule mod_deflate.c>
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/xjavascript
AddType x-font/otf .otf
AddType x-font/ttf .ttf
AddType x-font/eot .eot
AddType x-font/woff .woff
AddType image/x-icon .ico
AddType image/png .png
 
</IfModule>
 
Expires Header.
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?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>
 
<IfModule mod_expires.c>
      ExpiresActive on
      ExpiresByType image/gif "access plus 1 month"
      ExpiresByType image/jpeg "access plus 1 month"
      ExpiresByType image/png "access plus 1 month"
      ExpiresByType text/css "access plus 1 month"
      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
AddType image/x-icon .ico
<IfModule mod_header.c>
 
  <FilesMatch "\.ico$">
    # cache .ico files for 1 year(31536000 sec)
    Header set Cache-control max-age=31536000
  </FilesMatch>
 
</IfModule>
 
 
Made with help of Dr.Explain

Unregistered version