vhost工具箱
← 返回首页

.htaccess 生成器

HTTPS 强跳、重定向、缓存等 Apache 规则一键生成

强制 HTTPS

将所有 HTTP 请求跳转到 HTTPS

禁止目录浏览

防止直接访问目录列表

Gzip 压缩

压缩 HTML/CSS/JS 减少传输体积

防盗链

防止外站直接引用本站图片

Options -MultiViews
RewriteEngine On

# 强制 HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# 禁止目录浏览
Options -Indexes

# 自定义 404
ErrorDocument 404 /404.html

# Gzip 压缩
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/css application/javascript application/json
  AddOutputFilterByType DEFLATE text/xml image/svg+xml font/woff2
</IfModule>

# 图片缓存
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png  "access plus 1 month"
  ExpiresByType image/webp "access plus 1 month"
  ExpiresByType image/gif  "access plus 1 month"
</IfModule>

# 静态资源缓存
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/css                  "access plus 1 week"
  ExpiresByType application/javascript    "access plus 1 week"
  ExpiresByType application/x-font-woff   "access plus 1 week"
</IfModule>