Chuyển tới nội dung
Trang chủ » Hướng dẫn tăng tốc WordPress với file Htaccess

Hướng dẫn tăng tốc WordPress với file Htaccess

Tốc độ trang web là một yếu tố quan trọng trong SEO. Khi trang web của bạn tải chậm, người dùng sẽ cảm thấy khó chịu và có thể bỏ bạn mà đi. Trong bài viết này, mình sẽ hướng dẫn bạn một vài thủ thuật để tăng tốc WordPress với file .htaccess.

Theo một nghiên cứu chính thức của Google, các trang web tải dưới 1 giây có khả năng được xếp hạng cao hơn trong kết quả tìm kiếm.

Hướng dẫn tăng tốc WordPress với file .Htaccess
Hướng dẫn tăng tốc WordPress với file .Htaccess

Hãy bắt đầu từ những lưu ý với file htaccess trong quá trình bạn tối ưu hóa tốc độ trang web WordPress.

Lưu ý khi tăng tốc WordPress với Htaccess

Trước khi bắt đầu tối ưu, các bạn hãy chú ý những điều mình chỉ ra dưới đây:

  • Backup file Htaccess: Vì trong quá trình tối ưu có thể xảy ra lỗi khiến trang web của bạn bị lỗi. Khi đó bạn có thể restore lại file Htaccess để đưa website về trạng thái trước khi chỉnh sửa.
  • Hãy đảm bảo rằng bạn có thể chỉnh sửa file Htaccess. Hãy xem Cách tạo và chỉnh sửa file Htaccess
  • Sau khi hoàn tất hãy xóa toàn bộ cache và rreload lại trang để kiểm tra.
  • Cuối cùng, hãy kiểm tra lại trên các công cụ như PageSpeed, Pingdom hoặc GTmatrix để biết tốc độ website được cải thiện ra sao.

Tăng tốc WordPress bằng Htaccess

Bạn phải sử dụng các đoạn mã (code) chèn vào trong file .htaccess nằm trong thư mục gốc tài khoản hosting.

Bật nén Gzip và DEFLATE

Nén đầu ra (output) sử dụng Gzip hoặc Deflate. Giúp nén tất cả các file css, js, html trước khi gửi cho trình duyệt của khách truy cập.

  • Gzip Compression on Apache

Lưu ý: Code này chỉ hoạt động nếu module mod Gzip được kích hoạt trong web hosting server của bạn.

# QUYENDUONGIT START GZIP COMPRESSION
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
# QUYENDUONGIT END GZIP COMPRESSION
  • DEFLATE Compression on Apache

Hãy sử dụng đoạn code bên dưới nếu máy chủ của bạn hỗ trợ mod_deflate

# QUYENDUONGIT START DEFLATE COMPRESSION
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE "application/atom+xml" 
"application/javascript" 
"application/json" 
"application/ld+json" 
"application/manifest+json" 
"application/rdf+xml" 
"application/rss+xml" 
"application/schema+json" 
"application/vnd.geo+json" 
"application/vnd.ms-fontobject" 
"application/x-font" 
"application/x-font-opentype" 
"application/x-font-otf" 
"application/x-font-truetype" 
"application/x-font-ttf" 
"application/x-javascript" 
"application/x-web-app-manifest+json" 
"application/xhtml+xml" 
"application/xml" 
"font/eot" 
"font/otf" 
"font/ttf" 
"font/opentype" 
"image/bmp" 
"image/svg+xml" 
"image/vnd.microsoft.icon" 
"image/x-icon" 
"text/cache-manifest" 
"text/css" 
"text/html" 
"text/javascript" 
"text/plain" 
"text/vcard" 
"text/vnd.rim.location.xloc" 
"text/vtt" 
"text/x-component" 
"text/x-cross-domain-policy" 
"text/xml"
</IfModule>
# QUYENDUONGIT END DEFLATE COMPRESSION

Nếu mod_deflate không được hỗ trợ, hãy sử dụng đoạn code này:

<FilesMatch ".(txt|html|htm|php)">
php_value output_handler ob_gzhandler
  • Đối với NGINX

Sao chép và dán mã Htaccess sau vào tệp cấu hình của máy chủ web của bạn.

gzip on;
gzip_comp_level 2;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_disable "MSIE [1-6].(?!.*SV1)";
gzip_vary on;

Kích hoạt Keep Alive tăng tốc WordPress

Bật Keep Alive là một thủ thuật Htaccess mạnh mẽ khác để tăng tốc website WordPress. Nó cho phép máy chủ và trình duyệt web của bạn tải xuống tài nguyên trên một kết nối duy nhất, do đó nó làm tăng tốc độ trang.

Bạn có thể bật Keep Alive bằng cách sử dụng HTTP header Connection: Keep-Alive.

Hãy thêm mã Htaccess sau để bật Keep Alive và tăng tốc website của mình.

# QUYENDUONGIT START ENABLE KEEP ALIVE
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
# QUYENDUONGIT END ENABLE KEEP ALIVE
  • Bật Keep Alive trong NGINX

Keep Alive được bật theo mặc định trên máy chủ NGINX. Vì vậy bạn không cần làm gì cả.

Cho phép Cache trình duyệt

Sử dụng code bên dưới để cho phép cache trang web đối với các tập tin tĩnh như PHP, CSS và JS:

# QUYENDUONGIT - START EXPIRES CACHING #
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/x-icon "access 1 year"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 year"
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/ogg "access plus 1 year"
ExpiresByType video/webm "access plus 1 year"
ExpiresByType application/atom+xml "access plus 1 hour"
ExpiresByType application/rss+xml "access plus 1 hour"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType text/x-component "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType application/font-woff "access plus 1 month"
ExpiresByType application/font-woff2 "access plus 1 month"
ExpiresDefault "access 1 month"
</IfModule>
# QUYENDUONGIT - END EXPIRES CACHING #

Add Cache-Control Headers

# QUYENDUONGIT - BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch ".(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch ".(css)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch ".(js)$">
Header set Cache-Control "private"
</filesMatch>
<filesMatch ".(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</filesMatch>
</ifModule>
# QUYENDUONGIT - END Cache-Control Headers

Tắt Image Hotlinking

Đoạn code này sẽ chặn không cho hiển thị hình ảnh của bạn trên các website khác để giảm thiểu tiêu tốn tài nguyên dẫn tới tốc độ website giảm.

# QUYENDUONGIT – DISABLE IMAGE HOTLINKIING START
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?quyenduongit.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?google.com [NC]
RewriteRule .(jpg|jpeg|png|gif)$ – [NC,F,L]
# QUYENDUONGIT – DISABLE IMAGE HOTLINKIING END

Đừng quyên thay quyenduongit.com thành địa chỉ website của bạn nhé

Kích hoạt LightSpeed ​​Server Cache

Ngoài ra, nếu bạn đang sử dụng LightSpeed ​​Server để chạy trang web của mình giống như chúng tôi đã từng sử dụng LightSpeed ​​Server với AZDIGI . Bạn nên sử dụng mã Htaccess sau để bật bộ nhớ đệm cấp máy chủ. Điều này sẽ tăng tốc độ trang web WordPress của bạn lên đáng kể.

# QUYENDUONGIT – ENABLE LITESPEED CACHE START
<IfModule LiteSpeed>
CacheEnable public
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^GET|HEAD$
RewriteCond %{HTTP_HOST} ^sample.com|sample.net|sample.org [NC]
RewriteCond %{REQUEST_URI} !login|admin|register|post|cron
RewriteCond %{QUERY_STRING} !nocache
RewriteRule .* - [E=Cache-Control:max-age=300]
</IfModule>
# QUYENDUONGIT – ENABLE LITESPEED CACHE END

Đừng quên thay đổi sample.com bằng tên miền của riêng bạn. Nếu bạn không muốn bật bộ nhớ đệm trên nhiều miền, hãy xóa dấu gạch ngang (|) và các miền khác.

Chúc các bạn thành công!

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *