Generator .htaccess

Buat aturan redirect, cache, dan keamanan .htaccess Apache dengan pemeriksaan konflik

Opsi

URL kanonis
Host kanonis
Garis miring akhir

/blog/index.html dialihkan ke /blog/.

Pengalihan kustom
Pengalihan: 0

Belum ada pengalihan kustom. Tambahkan satu atau gunakan tempel massal.

.htaccess yang dihasilkan

# Generated by the CodingTool.dev .htaccess Generator for Apache 2.4.
# Needs AllowOverride AuthConfig FileInfo Indexes Options (or All); Apache answers 500 for directives it does not allow.
# Place this file in the document root. Test on a staging site before going live.

# Disable directory listings.
Options -Indexes

# Deny access to .htaccess, .htpasswd and other .ht files.
<FilesMatch "^\.ht">
  Require all denied
</FilesMatch>

# Deny backup, dump and editor swap files such as .bak, .old, .sql, .swp and ~ copies.
<FilesMatch "(\.(bak|old|sql|swp)|~)$">
  Require all denied
</FilesMatch>

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Block dotfiles and dot-folders such as .env and .git; keep /.well-known/ reachable.
  RewriteCond %{REQUEST_URI} !(^|/)\.well-known/
  RewriteRule (^|/)\. - [F]

  # Force HTTPS: redirect every HTTP request to the same host over HTTPS.
  RewriteCond %{HTTPS} !=on
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

<IfModule mod_headers.c>
  # Basic security headers. Adjust Permissions-Policy if your site uses these features.
  Header always set X-Content-Type-Options "nosniff"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
</IfModule>

# Compress text responses. AddOutputFilterByType needs mod_filter in Apache 2.4.
<IfModule mod_deflate.c>
  <IfModule mod_filter.c>
    # Text and markup
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
    # Scripts and data
    AddOutputFilterByType DEFLATE text/javascript application/javascript application/json application/ld+json application/manifest+json
    # Feeds and XML
    AddOutputFilterByType DEFLATE application/xml application/rss+xml application/atom+xml
    # SVG and uncompressed fonts
    AddOutputFilterByType DEFLATE image/svg+xml font/ttf font/otf application/vnd.ms-fontobject
  </IfModule>
</IfModule>

# Browser caching (conservative): short lifetimes that are safe without versioned file names.
<IfModule mod_expires.c>
  ExpiresActive On
  # HTML and data: always revalidate
  ExpiresByType text/html "access plus 0 seconds"
  ExpiresByType application/json "access plus 0 seconds"
  ExpiresByType application/ld+json "access plus 0 seconds"
  ExpiresByType application/xml "access plus 0 seconds"
  ExpiresByType text/xml "access plus 0 seconds"
  # Feeds
  ExpiresByType application/rss+xml "access plus 1 hour"
  ExpiresByType application/atom+xml "access plus 1 hour"
  # CSS and JavaScript
  ExpiresByType text/css "access plus 1 week"
  ExpiresByType text/javascript "access plus 1 week"
  ExpiresByType application/javascript "access plus 1 week"
  # Web app manifest
  ExpiresByType application/manifest+json "access plus 1 week"
  # Images
  ExpiresByType image/avif "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/svg+xml "access plus 1 month"
  ExpiresByType image/webp "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 month"
  # Fonts
  ExpiresByType font/woff "access plus 1 month"
  ExpiresByType font/woff2 "access plus 1 month"
  ExpiresByType font/ttf "access plus 1 month"
  ExpiresByType font/otf "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
  # Audio and video
  ExpiresByType audio/mpeg "access plus 1 month"
  ExpiresByType audio/ogg "access plus 1 month"
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/webm "access plus 1 month"
</IfModule>

.htaccess hanya berlaku jika pengaturan AllowOverride Apache mengizinkan direktif ini.

Disimpan sebagai htaccess.txt karena browser membuang titik di awal nama. Ganti namanya menjadi .htaccess di server.

Temuan

Tidak ada konflik
Galat: 0 · Peringatan: 0 · Pengalihan: 0

Catatan (3)

  • Catatan: Di belakang load balancer atau CDN yang mengakhiri TLS, %{HTTPS} bernilai mati dan aturan HTTPS bisa berputar. Tanyakan dulu ke penyedia hosting.

  • Catatan: Membutuhkan AllowOverride AuthConfig, FileInfo, Indexes, Options (atau All). Direktif di luar izin membuat Apache menjawab 500.

  • Catatan: Blok untuk mod_rewrite, mod_headers, mod_deflate, mod_filter, mod_expires dilewati diam-diam jika modulnya tidak dimuat.

Isi file ini

  • Opsi server
  • Perlindungan file
  • Aturan rewrite
  • Header respons
  • Kompresi
  • Cache browser

Galat: 0 · Peringatan: 0 · Pengalihan: 0

Apa yang dihasilkan

Generator ini menulis file .htaccess Apache 2.4 untuk kebutuhan umum pengalihan, performa, dan keamanan: paksa HTTPS, satu host kanonis, perapian garis miring akhir dan index.html, pengalihan 301/302/307/308 kustom, kompresi gzip, preset cache Expires, perlindungan daftar isi folder dan file tersembunyi, header keamanan dasar, HSTS opsional, serta halaman error kustom. Setiap bagian diberi komentar dan urutannya selalu sama sehingga perubahan mudah dibaca.

Pemeriksaan konflik

Sebelum ditulis, setiap pengalihan diperiksa: sumber yang salah format, protokol tujuan yang tidak aman atau tidak terduga, duplikat, pengalihan ke diri sendiri, putaran A ke B lalu kembali ke A, dan rantai yang menambah satu lompatan. Aturan yang akan berputar atau tidak pernah berjalan tidak dimasukkan ke file dan ditampilkan di Temuan. Host kanonis dan garis miring akhir berupa pilihan tunggal, jadi aturan yang bertentangan seperti tambah www dan hapus www tidak bisa digabung.

Detail Apache yang penting

Semua aturan rewrite berada dalam satu blok IfModule mod_rewrite dengan satu RewriteEngine On. Perubahan HTTPS dan host dilakukan dalam satu pengalihan, aturan garis miring akhir melewati file dan folder yang benar-benar ada, dan pengalihan garis miring atau indeks hanya berlaku untuk permintaan GET dan HEAD. Kontrol akses memakai Require all denied, bukan sintaks lama Order dan Deny. Blok yang bergantung pada modul dibungkus IfModule, sehingga modul yang tidak ada hanya melewati blok tanpa merusak situs.

Sebelum dipasang

.htaccess hanya berlaku jika pengaturan AllowOverride di server mengizinkan direktif ini; jika tidak, Apache menjawab 500. Letakkan file di document root karena path pengalihan kustom relatif terhadapnya. Di belakang proxy atau CDN yang mengakhiri TLS, aturan HTTPS tidak melihat skema asli dan bisa berputar. Aktifkan HSTS hanya setelah HTTPS berjalan di semua tempat, dimulai dengan max-age yang pendek. Browser menyimpan unduhan sebagai htaccess.txt; ganti namanya menjadi .htaccess di server. Semua proses berjalan di browser Anda dan tidak ada yang diunggah.

CodingTool resmi diluncurkan di

Launched on StartupBaseCodingTool.dev - Free, Fast & Easy Developer Tools in One Place | Product HuntCodingTool - Featured on Startup FameFazier badgeListed on Turbo0Featured on Twelve ToolsFeatured on Findly.toolsLaunchpadly Startup DirectoryCodingTool.dev — Featured on Smol Launch