Coding Tools

Htaccess Redirect Generator

Use this free Htaccess Redirect Generator to build Apache redirect rules for single pages, whole directories, HTTPS enforcement, and www normalisation. Copy production-ready rules and read the deployment guidance below before pasting anything into a live server. No sign-up required.

301 and 302 HTTPS and WWW 100% Free

Enter the old URL path (e.g., /old-page.html)

Enter the new URL (full or relative path)

Quick Reference

301 Redirect:

Permanent redirect. Tells search engines the page has moved permanently.

302 Redirect:

Temporary redirect. Page may return to original URL.

Exact Match:

Redirects only the specific URL entered.

Regular Expression:

Uses pattern matching for flexible redirects.

Why You Are Here

You have moved or removed pages. A restructure, a CMS migration, or content consolidation leaves old URLs that need pointing somewhere.

You are enforcing HTTPS or a canonical hostname. Serving the same site at both www and non-www, or over both HTTP and HTTPS, means four addresses for every page.

You are migrating a domain. The highest-stakes case, where a mistake costs traffic that took years to accumulate.

What This Tool Does

The Htaccess Redirect Generator builds Apache redirect rules from your inputs.

Choose your redirect type, enter source and destination, and the tool produces correctly formatted rules ready for your .htaccess file, including HTTPS enforcement and www normalisation patterns.

Rules are Apache-specific. They work on Apache and LiteSpeed. Nginx and IIS use entirely different configuration and do not read .htaccess at all.

Redirecting Is Not Rewriting

Worth stating plainly, because both are done with .htaccess and they are constantly confused.

 

Redirecting

Rewriting

The URL in the browser

Changes

Stays as typed

Requests

Two

One

Search engines see

A move

One URL

Use for

Content that has moved

Making a URL display differently

Redirects announce a move. Rewrites hide a mechanism

If the content still lives where it always did and you only want a prettier address, you want the URL Rewriting Tool instead.

301 or 302

 

301 Permanent

302 Temporary

Meaning

This has moved for good

This is a temporary detour

Ranking signals

Transfer to the destination

Do not transfer

Indexing

New URL replaces the old

Original stays indexed

Browser caching

AGGRESSIVE, often indefinite

Not cached

Use for

Migrations, restructures, HTTPS

A/B tests, maintenance, seasonal pages

Use 301 for anything permanent

A 302 on a permanent move leaves the old URL indexed and transfers nothing, so the destination starts from zero.

307 and 308 preserve the request method

A 301 or 302 can convert a POST into a GET when followed, which breaks form submissions and API calls. Use 308 for permanent and 307 for temporary where the method matters.

On signal transfer

Google has confirmed 301s pass ranking signals, though the transfer is not guaranteed to be exactly complete. A clean 301 to a genuinely relevant destination carries the large majority of a URL's value, and is vastly better than a broken page or a 302.

The 301 Caching Trap

This is the most practically important thing on the page and the least covered elsewhere.

Browsers cache 301 redirects aggressively, frequently for the lifetime of the browser profile

The whole point of permanent is that the browser stops asking. So a mistaken 301 does not go away when you fix it. A visitor who hits the wrong rule once keeps being redirected afterwards, because their browser never re-checks. From their side, your site is broken and clearing the cache is the only fix, which they will not think to do.

The consequence

You cannot reliably test a 301 on a live site. Your own browser will lie to you about whether the fix worked.

The sequence that avoids this

  1. Deploy the rule as a 302 first. Not cached, so every request re-evaluates.
  2. Verify it behaves correctly, including on the URLs you did not think about.
  3. Change it to 301 once you are confident.
  4. Test in a private window afterwards, since your normal browser may have cached something already.

Anyone already caught by a bad 301 needs to clear their browser cache. There is nothing you can do server-side to release them.

Never Redirect Everything to the Homepage

A common migration shortcut that actively causes harm, and one where most guides say do not without saying why.

Google treats mass redirects to the homepage as soft 404s

A redirect asserts that the destination is the equivalent of the original. When a hundred unrelated URLs all resolve to the homepage, that assertion is obviously false, so Google treats them as pages that no longer exist, while you have also removed the honest 404 that would have told users the same thing more clearly.

Situation

Correct handling

A direct equivalent exists

301 to that page

A close equivalent exists

301 to the nearest relevant page

The relevant category still exists

301 to the category

Nothing equivalent exists

404 or 410, NOT a redirect

Letting a page 404 is a legitimate answer

It is more honest than a redirect that misleads both users and search engines, and Google handles it cleanly.

Rule Order and CMS Conflicts

Apache reads .htaccess top to bottom and the first matching rule wins, which makes ordering functional rather than cosmetic. Specific rules must come before general ones. A broad catch-all near the top swallows every request before more specific rules get a chance.

WordPress, Laravel, and similar frameworks ship their own .htaccess block

Placing custom rules inside that block, or after it, breaks routing. The framework's catch-all sends everything to the front controller before your rules are reached.

# Custom rules go HERE, before the CMS block


# BEGIN WordPress

# ... framework rules ...

# END WordPress

Anything between the BEGIN and END markers may be overwritten

When the CMS regenerates the file, so custom rules placed there can silently disappear during an update.

Escaping and Common Syntax Errors

An unescaped dot matches any character

In a regex pattern, example.com also matches exampleXcom. Escape it with a backslash.

Character

Escape as

Dot

Backslash then dot

Question mark

Backslash then question mark

Plus sign

Backslash then plus sign

Parentheses

Backslash before each

Old paths are relative, destinations are absolute

In a Redirect directive, the source starts with a slash and omits the domain; the destination should be a full URL including protocol.

HTTPS rules need a condition or they loop

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


# The RewriteCond line is what prevents the loop.

Chains and Loops

A chain is A to B to C

Each hop adds latency, and search engines stop following after a number of hops. Collapse chains so the original points directly at the final destination, keeping the intermediate rules for URLs that enter the chain part-way.

A loop is A to B to A

Apache hits its internal limit and returns a 500 error. The most common cause is an HTTPS or www rule without a condition, as above.

Chains accumulate rather than being designed. An HTTPS migration, then a domain change, then a restructure, each adding rules on top of the last. Nobody sets out to build a four-hop redirect.

Redirect or RewriteRule

Two directives do this job and the simpler one is often the right choice.

 

Redirect (mod_alias)

RewriteRule (mod_rewrite)

Complexity

Simple

Powerful

Matching

Path prefix

Full regex

Conditions

None

RewriteCond

Best for

Single pages, whole directories

Patterns, conditional logic, HTTPS and www

Use Redirect when a simple path match will do

It is easier to read, harder to get wrong, and cheaper to evaluate. Reach for RewriteRule when you need pattern matching or conditions.

A Performance Note Nobody Mentions

Apache's own documentation recommends avoiding .htaccess files entirely where you have access to the main server configuration.

The reason is that .htaccess is read on every single request

Apache also checks for one in every directory along the path to the requested file, so a request several levels deep triggers several filesystem lookups before anything is served. Server configuration, by contrast, is read once at startup.

This is not a reason to avoid .htaccess on shared hosting

Where it is usually the only option available, and the cost on a small site is modest.

It is a reason to move rules into the virtual host configuration if you can

On a VPS, dedicated server, or any environment where you control Apache's configuration, the same rules run measurably faster there.

Common Mistakes to Avoid

Testing a 301 on a Live Site

Browsers cache 301 redirects aggressively and frequently indefinitely, so a mistaken rule keeps affecting anyone who hit it even after you correct the server. Your own browser will show you the cached behaviour rather than the current rule, making the fix appear not to work. Deploy as a 302, verify it, then switch to 301 once you are confident.
Redirecting Removed Pages to the Homepage

Mass redirects to the homepage are treated by Google as soft 404s, because the destination is plainly not equivalent to the original. The result is worse than a 404 for both users and search engines. Map each URL to its nearest genuine equivalent, use the category page where an exact match no longer exists, and let pages 404 or 410 where nothing relevant remains.
Editing .htaccess Without a Backup

A syntax error returns a 500 error for the entire site rather than only the affected URLs, and the error rarely identifies the offending line. Keep a copy of the working file before every change, deploy when a brief outage would be tolerable, and test on staging where possible. Recovering without a backup means rebuilding rules from memory while the site is down.

Quick Reference

Item

Detail

File location

Document root

Servers

Apache and LiteSpeed only

301

Permanent. 302 temporary

307 and 308

Preserve the request method

Browsers

Cache 301s, often indefinitely

Testing

Use 302, then switch to 301

Homepage redirects

Never redirect everything there

Why

That is a soft 404

Regex

Escape dots in patterns

HTTPS rules

Need RewriteCond or they loop

Rule order

Specific before general

CMS

Custom rules before the CMS block

Duration

Keep redirects indefinitely

Before editing

Back up every time

Frequently Asked Questions

What is an htaccess redirect generator?

An htaccess redirect generator builds Apache redirect rules from simple inputs so you do not have to write the syntax by hand. This Htaccess Redirect Generator produces 301 and 302 redirects for single pages and directories, along with HTTPS enforcement and www normalisation rules, ready to paste into your .htaccess file.

Should I use a 301 or a 302 redirect?

Use 301 for anything permanent, since it transfers ranking signals to the destination and replaces the old URL in the index. Use 302 only when the original URL will genuinely return, such as during maintenance or an A/B test. A 302 on a permanent move leaves the old URL indexed and passes nothing on.

Why does my redirect still happen after I removed the rule?

Because browsers cache 301 redirects aggressively, frequently for the lifetime of the browser profile. Once a browser has seen a permanent redirect it stops asking the server, so removing the rule has no effect for anyone already affected. Clearing the browser cache is the only fix, which is why testing with a 302 first is worth the extra step.

Can I redirect all my old pages to the homepage?

You can, but Google treats mass redirects to the homepage as soft 404s, because the destination is clearly not equivalent to the original page. The result is worse than a plain 404 for users and search engines alike. Redirect each URL to its closest genuine equivalent, and let pages return 404 or 410 where nothing relevant exists.

Why did my whole site go down after editing .htaccess?

A syntax error in .htaccess returns a 500 Internal Server Error for every request, not only the URLs the rule affects, and the message rarely identifies the line responsible. Restore your backup to bring the site back, then test the corrected rule on staging before deploying again.

How long should I keep redirects in place?

Indefinitely, wherever practical. External links, bookmarks, and printed references do not expire, so removing a redirect breaks every one of them permanently. A year is the commonly cited minimum, but there is rarely a good reason to remove a working redirect at all.

Do these rules work on Nginx?

No. .htaccess is an Apache feature, also supported by LiteSpeed. Nginx requires equivalent rules written into its own configuration with different syntax followed by a reload, and IIS uses web.config. A file pasted onto an Nginx host has no effect whatsoever.

Where exactly does the .htaccess file go?

In your site's document root, commonly named public_html, www, or htdocs. Rules apply to that directory and everything beneath it, and additional files in subdirectories add to or override the parent's rules for that subtree. The filename must be exactly .htaccess with the leading dot, saved as plain text. Rules from this Htaccess Redirect Generator go in the root file unless you have a specific reason otherwise.

Comments

Login to leave a comment

No comments yet. Be the first to comment!