.htaccess Generator

Generate production-ready .htaccess files for Apache servers. HTTPS redirects, security headers, caching, gzip compression, custom error pages, and more.

ApacheSecurity HeadersFree
Configuration Options
Redirects
Force HTTPS
Redirect all HTTP traffic to HTTPS
Force www
Redirect non-www to www
Remove www
Redirect www to non-www
Trailing Slash
Add trailing slash to URLs
Security
Security Headers
X-Frame-Options, X-Content-Type, XSS-Protection, HSTS
Block Directory Browsing
Prevent listing directory contents
Block Sensitive Files
Block .env, .git, .htaccess, etc.
Hotlink Protection
Prevent other sites from embedding your images
Performance
Gzip Compression
Compress text, HTML, CSS, JS, XML, JSON
Browser Caching
Set cache expiry headers for static assets
ETags
Disable ETags (use cache headers instead)
Other
Custom Error Pages
404, 403, 500 error pages
CORS Headers
Allow cross-origin requests (Access-Control-Allow-Origin)
PHP Settings
upload_max_filesize, memory_limit, etc.

Understanding .htaccess Files

The .htaccess (Hypertext Access) file is one of the most powerful configuration tools available to web developers and site administrators using Apache web servers. This distributed configuration file allows you to make configuration changes on a per-directory basis without needing root access to the server or editing the main httpd.conf file.

One of the most common uses of .htaccess is URL redirection. Forcing HTTPS ensures all traffic is encrypted, which is essential for security and now a ranking factor for search engines. Choosing between www and non-www (canonical URL) prevents duplicate content issues. These redirects use Apache's mod_rewrite module, which is enabled on virtually all modern Apache installations.

Security headers are another critical use case. Headers like X-Frame-Options prevent clickjacking attacks, X-Content-Type-Options prevents MIME-type sniffing, and HTTP Strict Transport Security (HSTS) tells browsers to always use HTTPS. Content Security Policy (CSP) headers can prevent cross-site scripting (XSS) and data injection attacks. These headers are recommended by security organizations like OWASP.

Performance optimization through .htaccess includes enabling gzip/deflate compression (which can reduce page size by 60-80%), setting browser cache expiry headers (so returning visitors load pages faster), and disabling ETags (which can cause issues with CDNs and load-balanced servers). These optimizations directly improve page load speed, user experience, and search engine rankings.

For websites hosted in Africa, where internet speeds can vary significantly, these performance optimizations are especially important. Proper caching and compression can make the difference between a usable and an unusable website for visitors on slower connections. Combined with a CDN, a well-configured .htaccess can dramatically improve your site's accessibility across the continent.

Frequently Asked Questions

Where do I put the .htaccess file?

Place it in your website's root directory (usually public_html or www). The file affects the directory it's in and all subdirectories. You can have multiple .htaccess files in different directories for directory-specific rules.

Why can't I see my .htaccess file?

Files starting with a dot (.) are hidden on Linux/Mac systems. In your FTP client, enable "Show hidden files" in settings. In cPanel File Manager, check "Show Hidden Files" in Settings.

Will .htaccess work on Nginx?

No. .htaccess is Apache-specific. Nginx uses its own configuration syntax in nginx.conf. If you're on Nginx, you'll need to convert these rules to Nginx format. Many hosting providers use Apache, but cloud servers often use Nginx.