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並列)。規則會套用於該目錄及其所有子目錄。

相關工具