Apache Rewrite Rule Generator

Build production-ready Apache mod_rewrite rules in seconds. Pick a rule type, fill in the inputs, and copy a clean .htaccess snippet for rewrites, redirects, and force-HTTPS.

Rule Type

Rule Options

Generated .htaccess Rules

Paste into your .htaccess file, or inside a <VirtualHost> / <Directory> block in your Apache config.

# Force HTTPS — 301 permanent
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

In .htaccess, the RewriteRule pattern is matched against the path without a leading slash. This tool strips it for you.

Requires mod_rewrite enabled (a2enmod rewrite) and AllowOverride All set for the directory.

About Apache Rewrite Rule Generator

The Apache Rewrite Rule Generator is a free online tool that builds copy-ready Apache mod_rewrite directives for the most common scenarios — forcing HTTPS, canonicalizing www vs non-www, 301/302 redirects for single pages, path prefixes, and full domain migrations, plus internal rewrites for pretty URLs and SPA / front-controller routing.

Apache's RewriteRule and RewriteCond directives are powerful but notoriously fiddly — the difference between an external redirect (the R flag, visible in the browser) and an internal rewrite (no R flag, URL stays put) trips up most people. This generator picks the right flags, anchors, and conditions for each rule type so the output is idiomatic and safe to paste.

All processing happens locally in your browser. Your domain names, paths, and patterns are never sent to a server.

How to Use Apache Rewrite Rule Generator

  1. 1. Pick a rule type — Force HTTPS, www canonicalization, trailing slash, domain move, single page, path prefix, pretty URL, front controller, regex, or custom.
  2. 2. For redirects, choose a status code. Use 301 for permanent moves (SEO-friendly, cached) and 302 for temporary ones. Use 307 / 308 when the HTTP method must be preserved.
  3. 3. Fill in the domain, paths, or regex pattern the selected rule type requires.
  4. 4. Decide whether to preserve the query string so UTM tags and tracking parameters survive the rule.
  5. 5. Keep Include RewriteEngine On enabled for a standalone snippet, or turn it off if it already appears earlier in your file.
  6. 6. Click Copy and paste into your site's .htaccess (in the web root) or an Apache config block.
  7. 7. Reload Apache with sudo systemctl reload apache2 (or httpd) and verify with curl -I https://yourdomain.com/old-path.

Common Use Cases

Force HTTPS site-wide

Redirect all HTTP traffic to HTTPS to satisfy HSTS, remove browser warnings, and meet SEO best practices.

Canonicalize www vs non-www

Pick one canonical hostname and 301 the other to prevent duplicate content and split link equity.

Pretty URLs for PHP apps

Rewrite /product/123 to product.php?id=123 internally so visitors and search engines see clean URLs.

SPA / front-controller routing

Send every non-file request to index.php or index.html so client-side routers and frameworks like Laravel work.

Domain migration

Move every URL from old-brand.com to new-brand.com while preserving paths and SEO authority.

Retire legacy URLs

301 deprecated pages and prefixes to their replacements so external backlinks still convert.

Frequently Asked Questions

What's the difference between a rewrite and a redirect?

A redirect uses the R flag (e.g. [R=301,L]) and tells the browser to request a new URL — the address bar changes. A rewrite has no R flag; Apache internally maps the request to a different file while the visitor's URL stays the same. Use redirects for moves, rewrites for pretty URLs and front controllers.

Where do I put these rules?

Put them in a .htaccess file in your site's document root, or directly inside a <VirtualHost> or <Directory> block in your main Apache config. Config-file rules are faster because Apache doesn't re-read them on every request, but .htaccess works without a restart.

Why isn't my rule working at all?

The two most common causes are that mod_rewrite isn't enabled (run sudo a2enmod rewrite and reload Apache), or that AllowOverride is set to None for your directory, which makes Apache ignore .htaccess entirely. Set AllowOverride All in the matching <Directory> block.

Why is there no leading slash in the pattern?

In a per-directory context (.htaccess), Apache strips the leading slash before matching the RewriteRule pattern. So you match ^old-page/?$, not ^/old-page/?$. This tool removes leading slashes from your input automatically.

How do I keep the query string?

For internal rewrites, add the QSA (Query String Append) flag to merge the original query with any you add in the substitution. For redirects, mod_rewrite passes the original query through by default — to drop it instead, append a ? to the target. This generator handles both via the "Preserve query string" toggle.

What do 307 and 308 do that 301/302 don't?

With 301/302, browsers may convert a POST into a GET when following the redirect. 307 (temporary) and 308 (permanent) guarantee the original method and body are preserved — important for APIs and form submissions.

What does the L flag mean?

L means "last" — Apache stops processing further rewrite rules in the current round once this one matches. It prevents later rules from interfering and is included on almost every rule. For complex chains you may also see END, which stops rewriting entirely.

Do redirects affect SEO?

Yes — positively when done right. A 301 passes nearly all link equity to the new URL. Avoid redirect chains (A → B → C), don't redirect to unrelated pages, and update your XML sitemap. After deploying, request reindexing in Google Search Console for the affected URLs.

Is my data sent to any server?

No. This tool runs entirely in your browser. Your domain names, paths, regex patterns, and rule configuration never leave your device.