What .htaccess does
An .htaccess file is a configuration file for the Apache web server, placed in a directory to control how the server behaves for that directory and everything beneath it. It handles redirects, forces HTTPS, sets caching rules, controls access and more — all without touching the server's main configuration, which is why shared hosting relies on it so heavily.
This generator produces the rules for the most common needs. Bear in mind it applies only to Apache; Nginx and other servers use entirely different configuration and will ignore an .htaccess file completely.
The most important rules
Forcing HTTPS redirects every insecure request to the secure version, which every site should do now that certificates are free and browsers flag unencrypted pages. Choosing between www and non-www and enforcing one consistently matters for both users and search engines, since serving the same content on both is a duplicate-content signal that splits ranking.
Custom redirects are the workhorse. When you move or rename a page, a 301 redirect sends both visitors and search engines to the new location and, crucially, passes the accumulated ranking value across. Without it, old links break and the ranking is lost.
Why 301 and not 302
The distinction matters. A 301 is a permanent redirect and tells search engines to transfer ranking to the new URL and update their index. A 302 is temporary and tells them to keep the old URL indexed. Using a 302 for a permanent move is a common mistake that quietly costs search visibility. For anything permanent, use 301, which is what this tool generates.
Performance rules
The caching and compression rules are worth including on almost any site. Browser caching tells returning visitors to reuse files they already have rather than downloading them again, which makes repeat visits dramatically faster. Gzip compression shrinks text-based files — HTML, CSS, JavaScript — before sending them, often by seventy percent, at negligible cost. Both improve the loading speed that affects both user experience and search ranking.
The rules that need caution
Two options here can cause problems if misapplied. Blocking hotlinking prevents other sites from embedding your images and consuming your bandwidth, but a rule that is too aggressive can block legitimate access, including from search engine image results and social media previews. Test it after applying.
Disabling directory listing is straightforwardly good — it stops the server displaying the contents of folders that have no index file, which is a minor information leak — and has no real downside.
Handle with care
A syntax error in .htaccess does not fail quietly; it typically brings down the entire site with a 500 error, because Apache refuses to serve anything when it cannot parse the file. Always back up the existing file before replacing it, change one thing at a time, and test immediately after each change. Keep the previous version to hand so you can revert instantly if a page breaks.
Generated in your browser
Rules are built locally with nothing transmitted.
Frequently Asked Questions
Does .htaccess work on any server?
No, only Apache. Nginx and other servers use different configuration and ignore .htaccess entirely.
What is the difference between 301 and 302 redirects?
A 301 is permanent and passes ranking to the new URL. A 302 is temporary and keeps the old URL indexed. Use 301 for permanent moves.
Should I force www or non-www?
Either works, but enforce one consistently. Serving content on both splits ranking as a duplicate-content signal.
Why did my site break after editing .htaccess?
A syntax error causes Apache to return a 500 error for the whole site. Always back up first, change one thing at a time, and test immediately.
Are caching and compression safe to enable?
Yes, both are broadly beneficial and safe. They speed up repeat visits and shrink text files with negligible downside.