cara mempercepat website laravel dengan htaccess
Mempercepat website Laravel dengan menggunakan .htaccess
dapat dilakukan dengan beberapa cara, termasuk mengaktifkan kompresi, mengatur caching, dan meminimalkan konten statis. Berikut ini beberapa langkah yang bisa diikuti:
- Mengaktifkan Kompresi Gzip:
Kompresi Gzip dapat mengurangi ukuran file yang dikirim dari server ke klien, sehingga mempercepat waktu muat halaman.
<IfModule mod_deflate.c>
# Kompresi HTML, CSS, JavaScript, Text, XML, dan font
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
</IfModule>
- Mengatur Cache Browser:
Mengatur cache browser untuk konten statis seperti gambar, CSS, dan JavaScript dapat membantu mengurangi waktu muat dengan menyimpan salinan file di browser pengguna.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
- Mengaktifkan Caching Konten Dinamis:
Memanfaatkan cache untuk konten dinamis dapat mengurangi beban server dan mempercepat waktu muat.
<IfModule mod_headers.c>
# Caching JSON
<FilesMatch "\.(json)$">
Header set Cache-Control "max-age=600, public"
</FilesMatch>
# Caching XML
<FilesMatch "\.(xml)$">
Header set Cache-Control "max-age=600, public"
</FilesMatch>
</IfModule>
- Menonaktifkan ETag:
Menonaktifkan ETag untuk mengurangi overhead HTTP.
<IfModule mod_headers.c>
Header unset ETag
</IfModule>
FileETag None
- Mengaktifkan Keep-Alive:
Mengaktifkan Keep-Alive untuk menjaga koneksi tetap terbuka untuk beberapa permintaan HTTP.
<IfModule mod_headers.c>
Header set Connection keep-alive
</IfModule>
- Minimalkan Konten Statis:
Pastikan file CSS dan JavaScript sudah diminimalkan (minified). Gunakan alat-alat seperti Laravel Mix untuk membantu dalam proses ini.
Contoh .htaccess
Lengkap:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/javascript application/rss+xml 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/xhtml+xml application/xml font/opentype font/otf font/ttf image/svg+xml image/x-icon text/css text/html text/javascript text/plain text/xml
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(json|xml)$">
Header set Cache-Control "max-age=600, public"
</FilesMatch>
Header unset ETag
Header set Connection keep-alive
</IfModule>
FileETag None
Pastikan untuk melakukan backup file .htaccess
sebelum melakukan perubahan dan uji website setelah modifikasi untuk memastikan tidak ada kesalahan konfigurasi.