.htaccess Redirect Generator

Generate Apache .htaccess rules for page redirects, HTTPS, www or non-www, trailing slashes, and domain moves. The rules are combined so each visitor gets a single redirect, not a chain.

Updated
Generated in your browser. Nothing is sent to your server.
Page redirects
.htaccess code
Paste the rules near the top of your .htaccess, above CMS rules.

How to use the .htaccess Redirect Generator

  1. Enter Your domain and choose the Redirect type. Use 301 for permanent moves.
  2. Tick the site-wide rules you want (Force HTTPS, www, Trailing slash, Move to new domain) and add a row under Page redirects for each old URL.
  3. Copy the rules from .htaccess code and paste them near the top of the .htaccess file in your site's root folder.

How it works

All rules use mod_rewrite and start with RewriteEngine On. Using one module for everything avoids the ordering surprises you get when mixing Redirect and RewriteRule.

  • Page redirects come first. The old path becomes an anchored pattern, such as ^old-page\.html/?$, with dots and other special characters escaped. The /? also catches the same URL with a trailing slash. A relative target is turned into a full URL on your preferred host, so the visitor lands on the final address in one step.
  • HTTPS and www are combined into one rule with [OR] conditions, so http://www.example.com goes straight to https://example.com.
  • Trailing slash rules skip real directories (!-d) so folder URLs keep working.
  • Each rule ends with [R=301,L]: redirect with the chosen code, then stop processing.

Examples

Domain example.com, force HTTPS, remove www, and redirect /old-page.html to /new-page:

RewriteEngine On

# Page redirects
RewriteRule ^old-page\.html/?$ https://example.com/new-page [R=301,L]

# HTTPS and non-www in one redirect
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

Which redirect code to use

CodeMeaningUse it when
301Moved permanentlyA page or site has moved for good. Search engines move rankings to the new URL.
302Found (temporary)The move is temporary, such as a sale page or maintenance.
307Temporary redirectLike 302, but the browser must keep the method and body (a POST stays a POST).
308Permanent redirectLike 301, but the method and body are kept.

Limitations

  • The rules need Apache with mod_rewrite and AllowOverride enabled. Nginx, IIS, and most Node hosts ignore .htaccess.
  • Behind a proxy or CDN that ends HTTPS (such as Cloudflare in Flexible mode), %{HTTPS} is always off and the HTTPS rule loops. Check %{HTTP:X-Forwarded-Proto} instead, or let the CDN force HTTPS.
  • Page redirects match the path only. To redirect on a query string, add a RewriteCond %{QUERY_STRING} line.
  • Test on a staging copy first, and clear your browser cache, because browsers cache 301 redirects.

Frequently asked questions

Where do I put the .htaccess file?

In your site's document root, the folder that holds index.php or index.html. Put these rules above any rules your CMS already has, such as the WordPress block.

Why is my redirect not working?

Check that mod_rewrite is enabled and that the server allows overrides. Also clear your browser cache, because an old 301 may be cached.

What is a redirect chain and why avoid it?

A chain is several redirects in a row, such as http → https → non-www → new page. Each hop adds delay, and search engines may stop following long chains. These rules send visitors to the final URL in one step.

Should I use 301 or 302 for SEO?

Use 301 for permanent moves. It tells search engines to index the new URL and pass the old page's ranking signals to it.

Often used together with the .htaccess Redirect Generator.

  • .htaccess Validator

    Checks .htaccess files for unknown directives, unclosed blocks, and bad rewrite rules.

  • Htpasswd Generator

    Creates .htpasswd lines with bcrypt, APR1-MD5, or SHA-1 hashes for Basic Auth.

  • Robots.txt Generator

    Creates robots.txt rules for search and AI crawlers and tests URLs against them.