OomTools
SEO

How to Set Up 301 Redirects in .htaccess (Cheatsheet & Examples)

Set up 301 redirects in .htaccess with copy-paste examples for HTTPS, WWW, and domain moves. Learn the syntax and generate error-free rules for free.

OomTools Editorial Team

Published 8 min read

How to Set Up 301 Redirects in .htaccess (Cheatsheet & Examples)

Sooner or later every website changes shape. You migrate to a new domain, restructure your URL hierarchy, rename a popular post, or finally move from HTTP to HTTPS. Each of those changes leaves old URLs pointing nowhere — and if you do nothing, visitors hit frustrating 404 errors and search engines quietly drop your indexed pages along with the ranking authority they had built.

The fix is the 301 redirect: a permanent instruction that sends both humans and crawlers from an old URL to its new home. On the Apache web servers that power a huge share of the internet, redirects live in a configuration file called .htaccess. This guide explains what a 301 does, gives you copy-paste snippets for the most common scenarios, and provides a directive reference so you can read and adapt the rules with confidence.

What is a 301 redirect?

A 301 is an HTTP status code meaning "this resource has moved permanently." It is different from a 302 ("moved temporarily"), and the distinction matters enormously for SEO. When a search-engine crawler encounters a 301, two things happen:

  1. It updates its index to show the new destination URL in place of the old one.
  2. It transfers roughly 90–99% of the link equity — the ranking authority accumulated by the old URL — to the new one.

A 302, by contrast, tells search engines the move is temporary, so they keep the old URL indexed and do not pass authority along. Using a 302 when you meant a 301 is one of the most common and costly redirect mistakes.

Before you edit: back up and be careful

.htaccess is powerful, and a single syntax error can take your whole site offline with a 500 error. Two rules keep you safe:

  • Always back up the existing .htaccess before changing it, so you can restore instantly if something breaks.
  • Place redirect rules near the top of the file, inside your server's root directory, before other rewrite logic that might intercept the request first.

Essential .htaccess 301 snippets

1. Redirect a single page to a new location

Use this when you rename a post's permalink or move one specific landing page:

Redirect 301 /old-page.html https://example.com/new-page

2. Force HTTP to HTTPS (SSL enforcement)

Send all traffic to the secure, encrypted version of your site:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

3. Force non-WWW to WWW (or the reverse)

Pick one canonical hostname to avoid duplicate-content issues where example.com and www.example.com are treated as separate sites:

# Force non-WWW to WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

To do the reverse (WWW to non-WWW), flip the condition and target accordingly.

4. Redirect an entire domain to a new domain

Essential during a rebrand or domain migration — it maps every path on the old domain to the same path on the new one:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-domain\.com [NC]
RewriteRule ^(.*)$ https://new-domain.com/$1 [R=301,L]

Directive reference

Understanding the pieces lets you adapt any rule rather than blindly pasting:

Directive What it does
RewriteEngine On Turns on Apache's mod_rewrite URL-manipulation engine
RewriteCond Defines a condition that must be true for the following rule to fire
RewriteRule Specifies the pattern to match and the destination to send it to
[R=301,L] R=301 forces a permanent redirect; L marks this the last rule to process

The [NC] flag you see in some conditions means "no case" — it matches regardless of upper/lowercase, which is useful for hostnames.

After you deploy: verify the redirect actually works

Setting a rule is only half the job; you must confirm the server returns the right status code. A redirect that silently returns a 302 instead of a 301, or that creates a redirect chain (old URL → intermediate → final) or a redirect loop, will bleed SEO value or break the page entirely. Check the response headers of your old URLs after deploying to confirm they return a clean, single 301 Moved Permanently pointing at the final destination. If your URLs contain special characters or query strings, encode them properly so the rules match as intended.

Common redirect pitfalls

  • Using 302 instead of 301 for a permanent move — authority never transfers.
  • Redirect chains — A → B → C forces extra round trips and dilutes equity. Redirect straight to the final URL.
  • Redirect loops — a misconfigured rule that sends a URL back to itself, producing an infinite loop and a broken page.
  • Redirecting everything to the homepage during a migration — always map old URLs to their true equivalents; a mass redirect to / is treated as a soft 404 by Google.

Generate error-free rules automatically

Hand-writing rewrite rules is where 500 errors are born — one misplaced caret or missing flag and the server chokes. Generating the rules from a form removes that risk, and pairing generation with header verification closes the loop.

Build and Verify Redirects Without the Risk

Generate valid Apache rules and confirm they work — all in your browser:

🔒 100% client-side privacy: code generation runs locally inside your browser. No server configuration is submitted to external endpoints.

Planning a site migration without losing rankings

The riskiest time for redirects is a full site migration — a new domain, a new CMS, or a restructured URL scheme. Done well, you keep your rankings; done carelessly, traffic can crater for months. A safe migration follows a checklist:

  1. Crawl the old site first and export every indexed URL so you have a complete inventory.
  2. Map each old URL to its true new equivalent — one to one, not a mass redirect to the homepage.
  3. Implement 301s for every mapped URL before launch.
  4. Keep the redirects live indefinitely. Search engines and old backlinks may reference the old URLs for years; removing the rules later resurrects the 404s.
  5. Update internal links to point directly at the new URLs, so you are not relying on redirects for your own navigation.
  6. Resubmit your sitemap and monitor coverage reports for crawl errors after launch.

The single biggest migration mistake is redirecting everything to /. Google treats a redirect to an irrelevant page as a soft 404 and passes little to no authority, so a lazy catch-all wastes the equity you were trying to preserve.

Redirect chains and loops: the silent killers

Two subtle problems undermine otherwise-correct redirects. A chain happens when URL A redirects to B, which redirects to C — each hop adds latency and dilutes link equity, and Google may stop following long chains entirely. Always redirect straight to the final destination. A loop happens when a misconfigured rule sends a URL back to itself or into a cycle, producing an endless redirect and a broken page. After deploying any non-trivial rule set, spot-check your important URLs to confirm each returns a single clean 301 to the final target with no intermediate hops.

When .htaccess is not the right tool

.htaccess is specific to Apache. If your site runs on Nginx, redirects live in the server config with different syntax; on a CDN or platform like Vercel or Netlify, they live in a config file or dashboard; and within a CMS like WordPress, a redirect plugin may be simpler and safer than editing server files directly. The 301 concept is identical everywhere — only the syntax changes — so understanding what a permanent redirect does matters more than memorizing one platform's format. Generate the rule for your stack, then verify the response code regardless of where the rule lives.

Frequently asked questions

Where is the .htaccess file located on my server?

Typically in your website's public root folder (/public_html/ or /www/). Because the name starts with a dot, it is a hidden file — enable "show hidden files" in your FTP client or file manager to see it.

What is the difference between a 301 and a 302 redirect?

A 301 means the move is permanent and transfers SEO authority to the new URL. A 302 means temporary and keeps the old URL indexed. Use 302 only for genuine short-term situations like maintenance or split tests.

Do 301 redirects slow down my site?

A single redirect adds a negligible extra request. The performance problem comes from redirect chains — always point the old URL directly at the final destination to avoid stacking hops.

Will a 301 redirect keep my Google rankings?

It preserves most of them. A correctly implemented 301 passes roughly 90–99% of the old page's link equity, so rankings usually recover quickly after search engines re-crawl the move.