Skip to main content

.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.

Reviewed for accuracy by the Math Ora X team Last updated

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

  1. Choose the rules you want, such as force HTTPS, redirect to www or non-www, and browser caching.
  2. Enter your preferred domain option if the tool asks for it, then review the selected checkboxes.
  3. Generate the .htaccess content and copy the result into your site root .htaccess file.
  4. Test the site in a browser to confirm the redirects and caching behave the way you expect.

Step by step, what happens

  1. The tool reads the options you tick and builds only the Apache directives needed for those features.
  2. It combines the redirect rules with the caching rules into one ready-to-use .htaccess block.
  3. 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.
  4. 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

  1. Tick the HTTPS option, select the www redirect, and enable caching.
  2. The generator creates Apache rules that redirect non-secure and non-www requests to the preferred URL and adds cache headers for static assets.
  3. 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.

Facebook Twitter WhatsApp