.htaccess Snippet Generator
Generate ready-to-use Apache .htaccess rules by ticking what you need: force HTTPS, redirect to www (or non-www), and enable browser caching.
About this tool
Generate ready-to-use Apache .htaccess rules by ticking what you need: force HTTPS, redirect to www (or non-www), and enable browser caching.
Example
Input: Force HTTPS + caching
Output: RewriteEngine On ... ExpiresActive On
Privacy
This tool runs entirely in your browser. Your data is processed locally and never uploaded to a server.
How to use this tool
- Choose the rules you want, such as force HTTPS, redirect to www or non-www, and browser caching.
- Enter your preferred domain option if the tool asks for it, then review the selected checkboxes.
- Generate the .htaccess content and copy the result into your site root .htaccess file.
- Test the site in a browser to confirm the redirects and caching behave the way you expect.
Step by step, what happens
- The tool reads the options you tick and builds only the Apache directives needed for those features.
- It combines the redirect rules with the caching rules into one ready-to-use .htaccess block.
- If you choose HTTPS and a domain preference, it adds the redirect logic in the correct order so requests are normalized before other rules apply.
- It shows the generated output instantly so you can copy it without editing the syntax by hand.
Worked example
Suppose you want every visit to your site to use HTTPS, go to the www version, and cache common static files for faster repeat visits.
Input: force HTTPS, redirect to www, enable browser caching
- Tick the HTTPS option, select the www redirect, and enable caching.
- The generator creates Apache rules that redirect non-secure and non-www requests to the preferred URL and adds cache headers for static assets.
- Copy the output into your .htaccess file at the site root and reload the page in a browser.
Output: RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
</IfModule>
Tips and common mistakes
- Make sure Apache allows .htaccess overrides on your server, or the rules will not take effect.
- Pick only one canonical domain style, either www or non-www, so you do not create redirect loops.
- Test the generated file on a staging site first if your site already has custom rewrite rules.
- After pasting the output, clear your browser cache when checking redirects or caching changes.
Frequently asked questions
What is .htaccess?
A per-directory configuration file for the Apache web server that controls redirects, caching and access rules.
Will these rules work on Nginx?
No, .htaccess is Apache-specific. Nginx uses its own config syntax.
Should I pick www or non-www?
Pick one canonical form and redirect the other to it; do not enable both at once.
More Developer Tools Tools
Explore related calculators in this category
API Key Generator
Generate random, secure API keys and tokens with an optional prefix. Cryptographically secure.
Bcrypt Hash Generator
Generate bcrypt password hashes in your browser with a configurable cost factor. Free online bcrypt tool.
CSRF Token Generator
Generate cryptographically secure random CSRF tokens for forms and APIs.
Cron Expression Generator
Build cron expressions from simple schedule choices and see a plain-English description. Free online cron builder.
You Might Also Like
Popular tools from other categories
Barcode Generator
Generate Code 128 / EAN barcodes from your data.
Color Palette Generator
Generate a harmonious color palette from one base color.
Generator Sizing Calculator
Estimate generator kW needed for your loads.
Hashtag Generator
Generate relevant hashtags from a topic keyword.
Can't Find the Right Calculator?
Try our AI Math Solver, type any problem in plain English and get instant step-by-step solutions.