When you rename pages, restructure a site or move to a new domain, every old URL that people have bookmarked, shared or linked to should lead somewhere useful. Without redirects they hit a 404 page, and the search rankings those URLs earned are lost. On Apache servers, which run most shared hosting, redirects live in a file called .htaccess.
A worked example
This list:
/old-page.html /new-page/
/products?id=42 /products/blue-kettle/
becomes rules like:
RewriteRule ^old-page\.html$ /new-page/ [L,R=301]
RewriteCond %{QUERY_STRING} ^id=42$
RewriteRule ^products$ /products/blue-kettle/? [L,R=301]
The dot in old-page.html is escaped so it only matches a real dot. The ? at the end of the second rule drops the old query string, so visitors don’t land on /products/blue-kettle/?id=42.
Avoid chains and loops
If page A redirects to B and B later redirects to C, visitors and crawlers make two hops. Each hop adds delay, and Google may stop following long chains. When you add a new redirect, update older ones to point straight at the final URL. The checks below the output flag chains and loops for you.
Before you upload
Keep a backup of your current .htaccess. A syntax error in this file can take the whole site offline with a 500 error, and restoring the backup fixes it instantly.
How to use the .htaccess Redirect Generator
- List your redirects, one per line: old path, new path or full URL, and an optional status code.
- Pick the default status, usually 301 for permanent moves.
- Choose whether to force HTTPS and how to handle www.
- Read the checks for duplicates, chains and loops.
- Copy the rules or download the
.htaccessfile.
Features
- 301, 302, 307 and 308 redirects, set per line or as a default.
- Handles old URLs with query strings like
/products?id=42using RewriteCond. - Force HTTPS and add or remove www in the same file.
- Escapes dots and other special characters in paths so rules match exactly.
- Detects duplicate sources, self-redirect loops and redirect chains.
Frequently asked questions
What is the difference between a 301 and a 302 redirect?
A 301 says the page has moved for good, so search engines move rankings to the new URL. A 302 says the move is temporary and the old URL should stay indexed. Use 301 for site migrations and renamed pages.
Where do I put the .htaccess file?
In the root folder of your website, usually public_html or www. If a .htaccess file already exists, add the new rules near the top rather than replacing it, especially on WordPress.
Why use RewriteRule instead of Redirect 301?
Both work for simple cases, but RewriteRule can match exact paths, handle query strings and combine with HTTPS and www rules in one consistent block. Mixing the two styles can cause rules to run in an unexpected order.
How do I test my redirects?
Open each old URL in a private window, or use a command such as curl -I https://example.com/old-page.html to see the status code and Location header. Clear your browser cache, because browsers remember 301s.
Written by Dewan Razib