100% Client-Side — Instant Apache Directives

.htaccess Generator

Generate production-ready Apache web server configuration rules for HTTPS redirection, Gzip compression, browser caching, and directory security.

Configuration Options

Redirect all HTTP traffic to secure HTTPS using a permanent 301 redirect.

Prevent visitors from listing files in directories without index pages.

Compress HTML, CSS, JavaScript, and SVG to reduce transfer payload by 60-80%.

Consolidate duplicate domain canonicalization for search engine indexing.

Show a custom HTML error page for missing URLs instead of default server errors.

Instruct visitors' browsers to cache static assets for faster repeat visits.

.htaccess
# ============================================
# .htaccess Generated by WebsiteHoste.com
# ============================================

# --- Force HTTPS/SSL ---
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# --- Redirect www to non-www ---
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# --- Disable Directory Browsing ---
Options -Indexes

# --- Enable Gzip Compression ---
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/json
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/woff
  AddOutputFilterByType DEFLATE font/woff2
</IfModule>

# --- Browser Cache-Control Headers ---
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 2592000 seconds"
  ExpiresByType text/html "access plus 0 seconds"
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"
  ExpiresByType font/woff "access plus 1 year"
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\.(css|js|jpg|jpeg|png|gif|webp|svg|woff|woff2|ttf|otf|ico)$">
    Header set Cache-Control "max-age=2592000, public"
  </FilesMatch>
  <FilesMatch "\.(html|htm)$">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
  </FilesMatch>
</IfModule>

What Is an .htaccess File? A Complete Guide for Webmasters

The .htaccess file (short for "hypertext access") is one of the most powerful configuration files available to website administrators running Apache web servers. This small but mighty plain-text file sits in the root directory of your website and controls everything from URL redirection and security policies to performance optimization and access control — all without requiring you to modify the main Apache server configuration or restart the web server.

Where to Place Your .htaccess File

The correct location for your primary .htaccess file depends on your hosting setup. On most cPanel-based shared hosting providers, you should place it inside the public_html directory (also sometimes called www or htdocs). This is the document root — the top-level folder from which Apache serves your website files.

Step-by-Step: Adding .htaccess via cPanel File Manager

  1. Log in to your hosting cPanel dashboard and open the File Manager application.
  2. Navigate to the public_html directory (your website root).
  3. Click Settings in the top-right corner and check "Show Hidden Files (dotfiles)" — this is essential because files starting with a dot are hidden by default.
  4. If an .htaccess file already exists, right-click and choose Edit. If not, create a new file named exactly .htaccess (with the leading dot, no file extension).
  5. Paste your generated rules, save the file, and test your website immediately to confirm the rules are working.

Frequently Asked Questions

An .htaccess (hypertext access) file is a directory-level configuration file used by Apache web servers to control server behavior on a per-directory basis. It allows you to manage URL redirects, enforce HTTPS, enable compression, set caching policies, block directory browsing, and configure custom error pages without modifying the main server configuration.
Place your .htaccess file in the root directory of your website, typically the public_html or www folder when using cPanel-based shared hosting. Make sure to check "Show Hidden Files" in cPanel File Manager settings, as files starting with a dot are hidden by default.
Yes, incorrect .htaccess syntax is one of the most common causes of 500 Internal Server Errors on Apache servers. Common mistakes include typos in directive names, using directives that require modules not enabled on your server (like mod_rewrite or mod_deflate), or creating infinite redirect loops. Always test your generated rules in a staging environment or keep a backup file.
No, .htaccess files are specific to the Apache web server. Nginx does not support .htaccess files natively. If you are running Nginx, you will need to add equivalent directives directly to your Nginx server block configuration (nginx.conf).
To force HTTPS, mod_rewrite inspects incoming HTTP traffic on port 80 and issues an HTTP 301 permanent redirect to the HTTPS (port 443) equivalent. This ensures all visitor sessions are TLS encrypted and consolidates search engine ranking signals to the secure URL.
Yes, Gzip compression via mod_deflate often shrinks HTML, CSS, JavaScript, and SVG to well under half their size over the wire. This directly lowers TTFB (Time to First Byte) and improves Google Core Web Vitals (FCP and LCP).