Guide

htaccess Redirects Explained: 301, 302, WWW, HTTPS and Rewrite Rules

A redirect sends visitors and search engines from one address to another. On Apache hosting, the .htaccess file is the usual place to set it up. This guide shows what each rule does and how to test it safely.

301 and 302 in plain words

A 301 redirect says a page has moved permanently. Search engines pass the old address to the new one over time. A 302 redirect says the move is temporary, so the old address stays the main one. Use 301 for a changed URL, a merged page or a domain move, and 302 for short tests or maintenance.

Common rules

Single page. Redirect 301 /old-page/ https://example.com/new-page/ moves one address.

Force HTTPS. A rewrite rule checks if the connection is not secure and sends the visitor to the https version. Choose www or non www. Pick one and redirect the other, so search engines see a single version of each page. Trailing slash. Keep one style so you do not create duplicate addresses.

The .htaccess Redirect Generator builds these rules from a form, so you do not need to remember the syntax.

Test before you rely on it

  1. Back up the current .htaccess file.
  2. Add one rule at a time.
  3. Open the old address in a private window and check where it lands.
  4. Look for redirect chains, where A goes to B and B goes to C. Point A straight to C.
  5. Avoid loops, which show a too many redirects error.

A mistake in .htaccess can take a site offline, so keep the backup ready.

Related files

After a move, update your Sitemap XML Generator output so it lists only the new addresses, and review the rules in Robots.txt Generator so you do not block the new pages. Then run the On Page SEO Checker on the new page to confirm the canonical link and title are correct.

This guide applies to Apache servers. Nginx and other servers use different syntax.

Frequently asked questions

What is a rewrite rule?

A rewrite rule changes how the server handles an address. A redirect rule sends the visitor to a new address, while an internal rewrite serves different content without changing the visible address.

Does a 301 redirect lose rankings?

A correct 301 helps search engines move to the new address. Rankings can shift for a while after a move, so monitor Search Console.

How do I force HTTPS with htaccess?

Add a rewrite condition that checks for a non secure request and redirect to the https version with a 301.

Where is the .htaccess file?

It sits in the root folder of your site, often called public_html, and may be hidden in some file managers.

More guides

Tools for this guide