Apache .htaccess 301 Redirect Guide & Rewrite Rules

A comprehensive developer guide to configuring server-side 301 and 302 redirects in Apache using .htaccess and mod_rewrite.

What is an Apache .htaccess Redirect?

An .htaccess file is an Apache web server configuration file that allows directory-level configuration changes. Configuring 301 (Permanent) redirects at the .htaccess level is the fastest and most SEO-friendly way to forward web traffic, preserve search rankings, and pass link equity (PageRank) to new URLs.

1. Redirect Single Page (mod_alias)

The simplest way to redirect a specific URL to another path or external website:

# Simple 301 Redirect for a single file Redirect 301 /old-page.html https://www.rapidtables.online/new-page.html # Relative redirect on the same domain Redirect 301 /calculator.html /calc.html

2. Redirect an Entire Directory / Folder

Redirect all requests inside a subfolder to a new directory while keeping file paths intact:

RedirectMatch 301 ^/old-folder/(.*)$ /new-folder/$1

3. Force HTTPS (SSL Encryption)

Ensure all visitors and search bots use the secure https:// protocol:

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

4. Force WWW Canonical Domain

Consolidate non-www traffic to www so search engines do not index duplicate versions:

RewriteEngine On RewriteCond %{HTTP_HOST} ^rapidtables\.online [NC] RewriteRule ^(.*)$ https://www.rapidtables.online/$1 [L,R=301]

5. Redirect Entire Old Domain to New Domain

When rebranding or migrating your website to a new domain name:

RewriteEngine On RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC,OR] RewriteCond %{HTTP_HOST} ^www\.old-domain\.com$ [NC] RewriteRule ^(.*)$ https://www.new-domain.com/$1 [L,R=301]
Need instant ready-to-use rewrite snippets?
Generate .htaccess, Nginx, PHP, and HTML redirect code automatically with our Redirect Code Generator.