Generator .htaccess untuk Apache

Buat aturan .htaccess Apache: paksa HTTPS, tambah atau hapus www, redirect 301, halaman error, gzip, cache browser, header keamanan, dan blokir dotfile.

Status bisa 301, 302, 303, 307, 308, atau 410 (dihapus). Path baru boleh berupa URL lengkap.

.htaccess
<IfModule mod_rewrite.c>
  RewriteEngine On
  # Redirect HTTP to HTTPS
  RewriteCond %{HTTPS} off
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# Redirects (mod_alias)
Redirect 301 /old-page /new-page
Redirect 301 /blog/2019 /blog

# Custom error pages
ErrorDocument 404 /404.html

# Disable directory listings
Options -Indexes

# Block access to hidden files such as .env and .git
<FilesMatch "^\.">
  Require all denied
</FilesMatch>
RedirectMatch 404 /\.git

# Security headers
<IfModule mod_headers.c>
  Header always set X-Content-Type-Options "nosniff"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# Compress text responses
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript
  AddOutputFilterByType DEFLATE application/javascript application/json application/xml application/rss+xml
  AddOutputFilterByType DEFLATE image/svg+xml font/ttf font/otf
</IfModule>

# Browser caching
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 month"
  ExpiresByType text/html "access plus 0 seconds"
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType text/javascript "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/avif "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType image/x-icon "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"
</IfModule>
Untuk Apache 2.4 dengan mod_rewrite, mod_alias, mod_headers, mod_deflate, dan mod_expires (blok dibungkus IfModule bila memungkinkan). Uji dulu di server staging – kesalahan sintaks di .htaccess membuat seluruh folder mengembalikan HTTP 500. Nginx tidak membaca .htaccess.

Apa itu Generator .htaccess?

Alat ini membuat file .htaccess siap pakai untuk Apache 2.4. Centang fitur yang Anda butuhkan – pengalihan HTTPS, host kanonis dengan atau tanpa www, HSTS, daftar isi direktori dimatikan, perlindungan file tersembunyi, kompresi gzip, cache browser, header keamanan dasar – lalu tambahkan pengalihan 301/302/410 dan halaman error kustom Anda sendiri.

Cara menggunakan Generator .htaccess

  1. Centang opsi yang Anda perlukan dan pilih cara penanganan www.
  2. Masukkan pengalihan sebagai /old-path /new-path, opsional diikuti status (301, 302, 303, 307, 308, atau 410).
  3. Masukkan path untuk halaman 404 dan 500 kustom.
  4. Salin atau unduh hasilnya sebagai .htaccess dan uji dulu di server staging.

Contoh

Masukan
Force HTTPS, remove www, redirect /old-page → /new-page
Keluaran
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
Redirect 301 /old-page /new-page

Kapan menggunakan Generator .htaccess

Gunakan di shared hosting atau server Apache tempat Anda tidak bisa mengedit konfigurasi server utama. Di Nginx, pengaturan bawaan LiteSpeed, Cloudflare, atau CDN, gunakan konfigurasi milik mereka – Nginx sama sekali mengabaikan .htaccess. Kesalahan sintaks menyebabkan HTTP 500 untuk seluruh direktori, jadi selalu lakukan pengujian.

Cara kerja Generator .htaccess

Aturan HTTPS dan www memakai mod_rewrite (RewriteCond %{HTTPS} off dan kondisi host) di dalam <IfModule mod_rewrite.c>; pengalihan www langsung memakai https jika HTTPS dipaksa, sehingga terhindar dari rantai dua langkah. Pengalihan path memakai baris Redirect dari mod_alias (Redirect gone untuk 410). Kompresi memakai mod_deflate, cache memakai mod_expires dengan masa berlaku panjang untuk aset statis dan tanpa cache untuk HTML, dan header memakai mod_headers dengan Header always set. Dotfile ditolak dengan <FilesMatch "^\."> dan RedirectMatch 404 /\.git.

Kasus Penggunaan Umum

  • Memaksa HTTPS dan satu host tanpa www setelah memasang sertifikat.
  • Mengalihkan URL lama setelah desain ulang situs.
  • Mengaktifkan kompresi dan cache untuk mempercepat situs WordPress di shared hosting.
  • Memblokir akses ke file .env dan .git.

Pertanyaan Umum

Mengapa muncul error 500 setelah mengunggah .htaccess?

Biasanya karena modul yang diperlukan belum aktif atau sebuah direktif tidak diizinkan oleh AllowOverride. Periksa log error Apache.

Apakah saya sebaiknya mengaktifkan HSTS?

Hanya jika seluruh situs dan semua subdomain sudah berfungsi lewat HTTPS – browser akan menolak HTTP biasa selama periode max-age.

Apakah .htaccess berfungsi di Nginx?

Tidak. Nginx tidak membaca .htaccess; aturannya harus diterjemahkan ke dalam blok server.

Jelajahi kategori lainnya