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.
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
- Deploy the rule as a 302 first. Not cached, so every request re-evaluates.
- Verify it behaves correctly, including on the URLs you did not think about.
- Change it to 301 once you are confident.
- 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
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.
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.
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 |
