跳至主要内容
.htaccess 生成器

.htaccess 生成器

切换您需要的规则 — 强制 HTTPS、www 或非 www 重定向、禁用目录列表、阻止点文件、启用 gzip 和浏览器缓存、自定义错误页面和 CORS — 并获得即用型 Apache .htaccess。立即复制或下载。

100% 客户端 · 没有数据离开您的浏览器

Force HTTPS

Redirect all HTTP traffic to HTTPS

Disable directory listing

Hide folder contents (Options -Indexes)

Block dotfiles

Deny access to .env, .git, etc.

Gzip compression

Compress text responses

Browser caching

Cache static assets

Custom error pages

404.html / 500.html

CORS: allow all origins

Access-Control-Allow-Origin: *

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

# Disable directory listing
Options -Indexes

# Block access to dotfiles (.env, .git, etc.)
<FilesMatch "^\.">
  Require all denied
</FilesMatch>

# Gzip compression
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/json image/svg+xml
</IfModule>

# Browser caching
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
</IfModule>

Tip: place this file in your site's root. Test on staging first — a bad rule can take a site offline.

如何创建 .htaccess 文件

生成用于重定向、性能和安全性的 Apache .htaccess 规则。

  1. 1

    选择选项

    切换您需要的规则,例如强制 HTTPS 或 gzip 压缩。

  2. 2

    审查

    当您更改选项时,.htaccess 会实时更新。

  3. 3

    部署

    将文件下载到您的站点根目录 - 首先在登台进行测试。