RevealTheme logo

.htaccess代码片段生成器

生成常见的.htaccess规则:强制HTTPS、重定向www、设置缓存头、启用Gzip。

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

如何使用本工具

  1. 1

    Open the dropdown and choose the rule you need (for example, Force HTTPS or Gzip compression).

  2. 2

    Read the generated snippet shown in the code box below the dropdown.

  3. 3

    Select the text and copy it, then paste it into your site's .htaccess file.

  4. 4

    Switch the dropdown to a different rule to view another snippet — combine several by pasting them one after another.

什么是.htaccess代码片段生成器?

.htaccess是Apache的目录级配置文件。它控制URL重写、重定向、缓存、压缩和安全头。Hostinger以及大多数共享主机都使用Apache(或兼容的LiteSpeed)。这些代码片段涵盖了最常见的需求。

常见使用场景

  • Forcing every visitor onto HTTPS after installing an SSL certificate on a shared Apache or LiteSpeed host.

  • Canonicalizing a domain to either the www or non-www version with a 301 redirect to consolidate SEO signals.

  • Adding a one-year browser cache to static assets so repeat visitors load CSS, JS, and modern image formats from cache.

  • Turning on Gzip compression for text responses to cut transfer size and improve page load time.

  • Hiding the contents of a folder that has no index file by disabling Apache's automatic directory listing.

  • Blocking PHP execution inside a public uploads directory so an attacker can't run a script they managed to upload.

常见问题

.htaccess在Nginx上能用吗?
不能:Nginx使用自己的配置语法。这些代码片段仅适用于Apache/LiteSpeed。
我应该把.htaccess放在哪里?
放在网站的根目录下(与index.html并列)。规则会应用于该目录及其所有子目录。

相关工具