Tech

htaccess generator

Turn on the sections you need and we build the Apache .htaccess file instantly: force HTTPS, www or non-www, 301 redirects, gzip, cache, security and blocking of sensitive files. Copy or download it, all in your browser.

Instant🔒In your browserNo signup
Live

Check the options you need. The file builds itself below, ready to copy or download.

Your .htaccess file

An .htaccess file is a configuration file that the Apache web server reads in each folder of your site. Without touching the server's global configuration, it lets you control how your website responds: redirect old URLs to new ones with 301 redirects, force HTTPS, unify the domain to www or non-www, enable gzip compression and browser cache so pages load faster, add security headers, and block access to private files such as .env or .git. It is one of the most powerful tools on shared hosting. One key detail: .htaccess is exclusive to Apache. If your server runs Nginx, this file is ignored entirely and you have to write the rules in nginx.conf instead. Before using it, confirm with your host that you are on Apache.

What an .htaccess is for

  • 301 redirects: move a page without losing your Google ranking.
  • Forced HTTPS: nobody enters over insecure http://.
  • www or non-www: stop Google from seeing two versions of the same site.
  • Speed: gzip shrinks files and cache avoids repeat downloads.
  • Security: block sensitive files and add headers that stop common attacks.
  • Custom errors: show your own 404 page instead of Apache's ugly default.

How redirects work

For simple path-to-path redirects the Redirect 301 /old /new directive is enough; it comes from the mod_alias module and needs no regular expressions. When the logic is more complex (force HTTPS, add or remove www, redirect by pattern) mod_rewrite steps in with RewriteCond and RewriteRule. That is where you use regex, and the most typical mistake is forgetting to escape the dot: in a regular expression a bare dot means "any character", so to represent a real dot you must write it as \\. with a backslash.

Reference: most-used .htaccess directives

DirectiveWhat it doesExample
RewriteEngineTurns the URL rewriting engine on or off. Goes before any RewriteRule.RewriteEngine On
RewriteCondAdds a condition that is evaluated before applying the RewriteRule that follows.RewriteCond %{HTTP_HOST} ^www\\.domain\\.com$ [NC]
RewriteRuleDefines a pattern and where to rewrite or redirect when it matches.RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirect 301Simple permanent redirect from one path to another (mod_alias, no regex).Redirect 301 /old /new
ErrorDocumentDefines a custom page for an HTTP error code.ErrorDocument 404 /404.html
Header setAdds or replaces an HTTP header on the response (mod_headers).Header set X-Frame-Options "SAMEORIGIN"
ExpiresByTypeSets how long the browser caches a given file type (mod_expires).ExpiresByType image/png "access plus 1 year"

Before you upload it

Always back up the .htaccess you already had: if the new one has a syntax error, Apache returns a 500 error across the whole site, not on a single page. With the backup you restore and recover instantly. Upload it via FTP or from your hosting file manager to the site root, and test an old URL to confirm it redirects correctly.

FAQ

Does .htaccess work on Nginx?

No. .htaccess is exclusive to Apache (and compatible servers like LiteSpeed). Nginx does not read it: its configuration goes in nginx.conf. If your hosting runs Nginx, you have to translate the rules to the Nginx format.

Where does the .htaccess file go?

In the root of your site, next to the index (for example in public_html). It affects that folder and all its subfolders. You can have several .htaccess files, one per folder.

Why is my redirect rule not working?

Usually mod_rewrite is disabled, RewriteEngine On is missing, AllowOverride is set to None, or there is a regex error. Remember the literal dot is written \\. with a backslash.

What is the difference between a 301 and a 302 redirect?

A 301 is permanent and passes SEO authority to the new URL. A 302 is temporary and does not transfer as much weight. For permanent moves always use 301.

Can I break my site with the .htaccess?

Yes: a syntax error returns a 500 error across the whole site. Back up the previous file before uploading a new one; if something fails, restore it and the site comes back instantly.

How do I test that my .htaccess works?

Open an old URL and check whether it redirects. To see the real code (301, 404, 500) use the browser Network tab or curl -I your-domain.com.

Do I need to enable mod_rewrite?

For HTTPS, www and RewriteRule redirects, yes. On almost all shared hosting it is already enabled. We wrapped those rules in <IfModule mod_rewrite.c> so that, if it is missing, they are ignored instead of breaking the site.

Does the filename start with a dot?

Yes, it is named exactly .htaccess, with the dot at the start and no extension. Be careful when downloading it on Windows: make sure it does not end up as htaccess.txt.

Was this generator useful?