.htaccess Generator: Apache Config
.htaccess rules for redirects, HTTPS, caching, gzip, CORS, and basic auth — with working examples.
Published:
Tags: htaccess generator, Apache htaccess file, htaccess redirect rules
.htaccess Generator: Apache Config The .htaccess file is the primary configuration interface for Apache shared hosting. It controls redirects, HTTPS enforcement, compression, caching, security headers, and access rules — one file, dozens of behaviors. --- What .htaccess Can Do? | Feature | Requires | |---------|---------| | 301/302 redirects | modrewrite or Redirect directive | | HTTP → HTTPS | modrewrite | | www → non-www | modrewrite | | Gzip compression | moddeflate | | Browser caching | modexpires | | Security headers | modheaders | | Basic authentication | modauthbasic | | Block specific IPs | modauthzhost | | Custom error pages | ErrorDocument directive | | CORS headers | modheaders | Most shared hosting environments have all of these modules enabled. On VPS or cloud, check with .…
Frequently Asked Questions
What is an .htaccess file?
An .htaccess file is a per-directory configuration file for Apache HTTP Server. It lets you change server behavior for a specific directory and its subdirectories without editing the main httpd.conf file. Common uses include redirects, HTTPS enforcement, caching, and access control.
How do I redirect HTTP to HTTPS with .htaccess?
Use mod_rewrite to check if the connection is not HTTPS and redirect to the HTTPS version: `RewriteEngine On`, `RewriteCond %{HTTPS} off`, `RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]`. Use the free [.htaccess Generator](https://theproductguy.in/tools/htaccess/-generator/) to produce this cleanly.
How do I enable gzip compression in .htaccess?
Load mod_deflate and add `AddOutputFilterByType DEFLATE text/html text/css application/javascript`. This compresses text-based responses before they are sent to the browser, typically reducing transfer size by 60–80%.
How do I add cache headers with .htaccess?
Use `mod_expires` with `ExpiresActive On` and `ExpiresByType` directives to set cache lifetimes per MIME type. CSS and JS can be cached for one year when files use content-hash naming; HTML should be short or no-cache.
How do I add CORS headers with .htaccess?
Use `mod_headers` with `Header set Access-Control-Allow-Origin "*"` for public APIs. For production, replace `*` with the specific allowed origin, e.g. `"https://app.example.com"`, to prevent cross-origin data exposure.
All articles · theproductguy.in