Home / Configs / Servers / .htaccess file

.htaccess generator: HTTPS redirect, www, caching and more

Build an Apache .htaccess file with an HTTPS redirect, www rules, compression, caching and a front controller.

For Apache 2.4 with mod_rewrite. Many shared hosts allow it. Not for nginx or IIS.

An .htaccess file lets you change how Apache behaves for one site without touching the main server config. Tick what you need, then save the result as .htaccess in your site's top folder.

Create your config file

Type your own values. The file below updates as you type, and nothing you type leaves your browser.

These redirects send visitors to https://, so turn on HTTPS first
Only needed when you add www

.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On

# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

# No folder listings
Options -Indexes

# Hide dotfiles and .git
<FilesMatch "^\.">
  Require all denied
</FilesMatch>
RedirectMatch 404 "/\.git"

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/javascript application/json application/xml image/svg+xml
</IfModule>

<IfModule mod_expires.c>
  ExpiresActive On
  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 text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
</IfModule>

How this setup works

Diagram: Browser, then Apache, then Rules run
Browser (asks for a page) → Apache (reads .htaccess) → Rules run (redirect, compress, cache)

Step-by-step setup

  1. Save the lines above as .htaccess (with the dot) in your site's top folder, next to index.php or index.html.
  2. Make sure Apache allows it: the site's config needs AllowOverride All, and mod_rewrite must be on.
  3. Open your site in a private window, so old redirects in the browser cache do not confuse the test.
  4. If you see a 500 error, read /var/log/apache2/error.log. It names the line that failed.
  5. Turn on one option at a time if something breaks, to find the line that causes it.

What the important lines mean

Common errors and how to fix them

500 Internal Server Error after saving the file
A line is not allowed or a module is off. Read the Apache error log. Common causes are AllowOverride too strict for Options, or mod_rewrite, mod_expires or mod_deflate not enabled.
ERR_TOO_MANY_REDIRECTS
HTTPS is handled by a proxy, CDN or load balancer, so Apache always sees plain HTTP. The X-Forwarded-Proto line covers most setups. Also check the www rule is not fighting another one in your CMS.
My .htaccess changes do nothing
The server ignores the file. Set AllowOverride All for your site folder and reload Apache. Nginx never reads .htaccess.
Every page shows 404 after adding index.php routing
Your app must handle the path itself, and the file must really be called index.php in the same folder. Check that mod_rewrite is enabled.

Frequently asked questions

Where do I put the .htaccess file?
In the top folder of your website, the one that holds index.php or index.html. A file in a subfolder only affects that folder.
How do I redirect HTTP to HTTPS in .htaccess?
Use RewriteCond %{HTTPS} off followed by a RewriteRule to https://%{HTTP_HOST}%{REQUEST_URI} with R=301, as in the generated file.
Does .htaccess work on nginx?
No. Nginx ignores .htaccess files. You put the same rules in the nginx server block instead.

Written for: Apache 2.4 · Updated 2026-10-11

Free tools that help

Fix a problem with this setup

More server configs

See all 41 config generators