.htaccess generator
Create an .htaccess file with https, www, 301 redirects, custom error pages, file protection, and caching.
Address
Security and performance
301 redirects
Custom error pages
.htaccess
# Hide directory listings for folders with no index page
Options -Indexes
RewriteEngine On
# Force HTTPS and the address without www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
# Block access to hidden files and folders such as .env and .git
RewriteRule (^|/)\.(?!well-known/) - [F,L]
# Block access to backups, logs, and database dumps
<FilesMatch "\.(bak|old|orig|swp|sql|log|sh|ini)$">
Require all denied
</FilesMatch>
# Compress text before sending
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript application/javascript application/json application/xml image/svg+xml
</IfModule>
# Let the browser cache images, fonts, CSS and JavaScript
<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/avif "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
</IfModule>
What these lines do
- Options -Indexes prevents visitors from seeing a list of files in a folder that has no index file.
- Visitors arriving via HTTP are sent with a permanent redirect (301) to HTTPS. The certificate must be installed first, otherwise visitors will see a warning.
- All visitors end up on the same version of the address. This avoids duplicate content in Google and ensures that logins and cookies behave consistently everywhere.
- Files starting with a dot, such as .env and .git, along with backups and logs, become inaccessible from outside. The .well-known folder is left open, as it is needed for SSL certificates.
- Compression makes HTML, CSS and JavaScript significantly smaller to transfer, so pages load faster. LiteSpeed often handles this automatically, in which case these lines do no harm.
- The browser caches images and fonts for one year, and CSS and JavaScript for one month. If you replace a file but keep the same name, visitors may see the old version until the cache expires. Rename the file or append ?v=2 when you make changes.
About this tool
.htaccess is a configuration file for web servers running Apache or LiteSpeed. It sits in your website's root folder and controls redirects, error pages, access, and caching — without needing to change anything in the server's own settings.
Choose what you need and the tool writes the lines for you, with a comment and explanation for each section. Everything is generated in your browser. Copy the text and place it at the top of your .htaccess file.
Always save a copy of the old file before making changes. A typo in .htaccess typically causes a 500 error across the entire site — and the fix is simply to restore the backup. If you're using WordPress, your lines should go above the block that starts with # BEGIN WordPress.
Frequently asked questions
Where do I find .htaccess?
In your website's root folder, usually public_html. The file starts with a dot and may be hidden. Enable hidden files in your file manager or FTP client.
The site gives a 500 error after the change. What do I do?
Restore the old file. Then look for typos, or add one rule at a time to identify which one is causing the error.
Does .htaccess work with LiteSpeed?
Yes. LiteSpeed reads .htaccess in the same way as Apache, using the same rules for redirects and access control.
Does it work with Nginx?
No. Nginx does not read .htaccess. Rules are added directly to the server's own configuration instead.