ToolPal

.htaccess 生成器

生成用于重定向、缓存、安全和性能的 Apache .htaccess 配置文件。选择所需规则并复制即用配置。

重定向

性能

安全

其他

.htaccess
# Generated by ToolBox Hub — https://toolboxhubs.com
Options -MultiViews

# Redirect HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Enable Gzip Compression
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# Browser Caching
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpg "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/webp "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"
  ExpiresByType text/html "access plus 1 day"
</IfModule>

# Disable Directory Listing
Options -Indexes

# Block access to .htaccess
<Files .htaccess>
  Order allow,deny
  Deny from all
</Files>

如何使用 .htaccess

将 .htaccess 文件放置在 Apache 网络服务器根目录(通常是 public_html 或 www)中。确保已启用 mod_rewrite 和 mod_headers。

使用方法

生成用于重定向、缓存、安全和性能的 Apache .htaccess 配置文件。选择所需规则并复制即用配置。

  1. 1Select your redirect rules: enable HTTP to HTTPS if your site has an SSL certificate (it should). Choose www to non-www or vice versa based on your canonical domain. Avoid enabling both www and non-www redirects simultaneously — pick one direction.
  2. 2Enable performance options: Gzip compression typically reduces HTML/CSS/JS size by 60-80%. Browser caching sets long expiry headers for images and assets so returning visitors don't re-download them. Both are safe to enable for almost any Apache site.
  3. 3Configure security rules: Disable directory listing to prevent visitors from browsing your file system. Block .htaccess access to hide your server configuration. Add security headers (X-Frame-Options, XSS Protection) for an extra layer of protection against common attacks.
  4. 4Enter your domain name in the config field if you're using non-www redirects or the hotlink protection rule — it needs to know which domain to allow. Click Copy and place the file in your web root (public_html, www, or wherever index.php/index.html lives).

主要功能

  • 14 configurable rules across 4 categories — redirects, performance, security, and other directives
  • Real-time generation — the .htaccess output updates instantly as you toggle rules
  • Conflict detection — enabling both www→non-www and non-www→www simultaneously is prevented
  • Custom domain and 404 page inputs — personalizes hotlink protection and error page rules

常见问题

了解更多