⚡
usefultools.dev
.ht

.htaccess Redirect Generator – 301, HTTPS, www

NEW

.htaccess redirect generator for Apache: force HTTPS, www or non-www, trailing slashes, 301/302 redirects and clean URLs as commented mod_rewrite rules.

Runs 100% in your browser — your input is never uploaded or stored on our servers.

Site-wide rules
  • info: Browsers cache 301 redirects hard. Test with 302 first, then switch to 301 when the rules are correct.
  • info: Rule order matters: Apache runs rules top to bottom and [L] stops the current pass. Keep specific redirects above the site-wide HTTPS/www rules, and internal rewrites last.
  • info: Behind a load balancer or Cloudflare, %{HTTPS} may always be "off". Use RewriteCond %{HTTP:X-Forwarded-Proto} !https instead to avoid a redirect loop.

Generated .htaccess.htaccess

# .htaccess generated by UsefulTools.dev
# Place it in the document root. Requires mod_rewrite (AllowOverride FileInfo or All).

RewriteEngine On

# 1. Single redirects (most specific first, before the site-wide rules)
RewriteRule ^old-page/?$ /new-page [R=301,L]
RewriteRule ^blog/2019/hello/?$ /blog/hello-world [R=301,L]
RewriteCond %{QUERY_STRING} ^id=42$
RewriteRule ^product\.php/?$ /products/blue-widget? [R=301,L]
RewriteRule ^promo/?$ https://shop.example.com/sale [R=302,L]

# 2. Force HTTPS and non-www in a single 301 hop
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

# Custom 404 page
ErrorDocument 404 /404.html
⚡

Quick Examples

Typical inputs and the results this tool produces.

301
/old-page /new-page
RewriteRule ^old-page/?$ /new-page [R=301,L]
Query
/product.php?id=42 /p
RewriteCond %{QUERY_STRING} ^id=42$
One hop
HTTPS + non-www
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
Check
/a /a
Error: would loop forever
📖

Overview

Apache reads per-directory rules from a .htaccess file, and most redirects there are written with mod_rewrite. This .htaccess redirect generator writes those rules for you: force HTTPS, redirect www to non-www (or the other way) in a single 301 hop, add or remove trailing slashes, serve /page from page.html, set a custom 404 page and add any number of single 301 or 302 redirects from an old path to a new one.

Rules are emitted in a safe order with a comment on each block, specific redirects first and internal rewrites last. The generator warns about redirect loops, chains, duplicate sources and setups that loop behind a proxy. It runs entirely in your browser.

Why use this tool?

  • No more hand-escaping mod_rewrite regex
  • HTTPS and www handled in one redirect, not two
  • Old URLs with query strings redirected correctly
  • Warnings for loops, chains and rule order
  • Commented output you can review before deploying
⭐

Features

🔒

Force HTTPS

Redirects every plain-HTTP request to HTTPS, combined with the www rule when both are on.

🌐

www or non-www

Pick one canonical host; the other one gets a permanent redirect.

↪️

Bulk 301 / 302

Paste "old new [code]" lines; full old URLs and query strings are supported.

🧼

Clean URLs

Remove .html extensions and add or remove trailing slashes.

⚠️

Order warnings

Flags loops, redirect chains, duplicate sources and risky combinations.

📄

Custom 404

Adds an ErrorDocument line and warns if you use a full URL.

🛠️

How to Use

1

Pick site-wide rules

Tick Force HTTPS, choose the www handling and trailing slash mode.

2

List redirects

Enter one "old new" pair per line, optionally followed by 301 or 302.

3

Read the warnings

Fix errors and chains shown above the output.

4

Download and test

Download .htaccess, upload it to the document root and test with curl -I.

🤔

Frequently Asked Questions