.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 的循环,以及多跳一次的重定向链。会造成循环或永远不会执行的规则不会写入文件,并列在“发现”中。规范主机和末尾斜杠都是单选,因此无法同时启用添加 www 和去掉 www 这类互相矛盾的规则。

重要的 Apache 细节

所有重写规则共用一个 IfModule mod_rewrite 区块,只有一个 RewriteEngine On。HTTPS 和主机变更在一次重定向内完成,末尾斜杠规则会跳过真实存在的文件和目录,斜杠和首页重定向只作用于 GET 和 HEAD 请求。访问控制使用 Require all denied,绝不使用过时的 Order 和 Deny 语法。依赖模块的区块都包在 IfModule 中,缺少模块时只会跳过该区块,而不会让网站出错。

部署之前

.htaccess 只在服务器的 AllowOverride 设置允许这些指令时生效,否则 Apache 会返回 500。请把文件放在文档根目录,因为自定义重定向的路径相对于它。在终止 TLS 的代理或 CDN 之后,HTTPS 规则看不到原始协议,可能陷入循环。只有在 HTTPS 全面可用后再开启 HSTS,并从较短的有效期开始。浏览器会把下载保存为 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