.htaccess ジェネレーター

Apache の .htaccess リダイレクト、キャッシュ、セキュリティ規則を競合チェック付きで作成

オプション

正規 URL
正規ホスト
末尾スラッシュ

/blog/index.html は /blog/ にリダイレクトされます。

カスタムリダイレクト
リダイレクト: 0

カスタムリダイレクトはまだありません。追加するか、一括貼り付けを使ってください。

生成された .htaccess

# 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 は、Apache の AllowOverride 設定がこれらのディレクティブを許可している場合にだけ有効です。

ブラウザが先頭のドットを削除するため htaccess.txt として保存されます。サーバー上で .htaccess に名前を変更してください。

検出結果

競合は見つかりません
エラー: 0 · 警告: 0 · リダイレクト: 0

メモ (3)

  • メモ: TLS を終端するロードバランサーや CDN の背後では %{HTTPS} が off になり、HTTPS ルールがループすることがあります。事前にホスティング会社へ確認してください。

  • メモ: AllowOverride AuthConfig, FileInfo, Indexes, Options(または All)が必要です。許可されていないディレクティブがあると Apache は 500 を返します。

  • メモ: mod_rewrite, mod_headers, mod_deflate, mod_filter, mod_expires が読み込まれていない場合、そのブロックは何も言わずにスキップされます。

このファイルの内容

  • サーバーオプション
  • ファイル保護
  • 書き換えルール
  • レスポンスヘッダー
  • 圧縮
  • ブラウザキャッシュ

エラー: 0 · 警告: 0 · リダイレクト: 0

生成される内容

このジェネレーターは、よくあるリダイレクト、パフォーマンス、セキュリティの要件に合わせて Apache 2.4 の .htaccess を書き出します。HTTPS の強制、正規ホストの統一、末尾スラッシュと index.html の整理、カスタムの 301/302/307/308 リダイレクト、gzip 圧縮、Expires キャッシュのプリセット、ディレクトリ一覧とドットファイルの保護、基本的なセキュリティヘッダー、任意の HSTS、独自のエラーページに対応します。各セクションにはコメントが付き、順序は常に同じなので差分が読みやすくなります。

競合チェック

各リダイレクトは書き出す前に、転送元の形式、安全でないか想定外の転送先プロトコル、重複、自分自身へのリダイレクト、A から B へ行って A に戻るループ、余分な 1 回を生むチェーンを確認します。ループするルールや実行されないルールはファイルから除外し、「検出結果」に表示します。正規ホストと末尾スラッシュは単一選択なので、www を付けると外すのような矛盾するルールは同時に選べません。

押さえておきたい Apache の細部

すべての書き換えルールは 1 つの IfModule mod_rewrite ブロックにまとまり、RewriteEngine On は 1 回だけです。HTTPS とホストの変更は 1 回のリダイレクトで行い、末尾スラッシュのルールは実在するファイルとフォルダーを除外し、スラッシュとインデックスのリダイレクトは GET と HEAD のリクエストにだけ適用します。アクセス制御には Require all denied を使い、古い Order と Deny の構文は使いません。モジュールに依存するブロックは IfModule で囲むため、モジュールがなくてもサイトは壊れず、そのブロックがスキップされるだけです。

公開する前に

.htaccess は、サーバーの AllowOverride 設定がこれらのディレクティブを許可している場合にだけ有効で、許可されていなければ Apache は 500 を返します。カスタムリダイレクトのパスはドキュメントルートからの相対パスなので、ファイルはドキュメントルートに置いてください。TLS を終端するプロキシや CDN の背後では、HTTPS ルールが元のスキームを判別できずループすることがあります。HSTS は HTTPS がすべての場所で動いてから、短い max-age で有効にしてください。ブラウザはダウンロードを htaccess.txt として保存するので、サーバー上で .htaccess に名前を変えてください。処理はすべてブラウザ内で行われ、何もアップロードされません。

CodingToolが正式に公開されました

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