.htaccess Snippet Generator
Build an .htaccess file for an Apache website: force HTTPS and www in one redirect, add 301 redirects and error pages, and switch on compression, caching and security headers.
About this tool
.htaccess is a settings file for Apache, the web server used by most shared hosting, including cPanel hosts. Tick what you need and the tool writes one file with a comment above each section. The HTTPS and www rules are combined so visitors are redirected once, not twice, which is faster and better for search rankings. If your host sits behind Cloudflare or another proxy, tick the proxy option, otherwise the HTTPS rule can redirect forever.
Each section is wrapped in an <IfModule> check, so a module your host doesn’t have is skipped instead of breaking the site. Blocking hidden files stops anyone downloading a stray .git folder or .env file with passwords in it, while still allowing .well-known, which SSL certificates need. HSTS is off by default because it can’t easily be undone: browsers remember it for a year.
Back up your existing .htaccess before replacing it, and test the site straight after uploading; a mistake shows as a 500 error, which you fix by restoring the backup. The download is called htaccess.txt because many computers hide files starting with a dot; rename it to .htaccess on the server. Nginx and IIS servers don’t read .htaccess files. Your choices are remembered on this device.