.htaccess Redirect Guide
How to write 301, 302, and custom redirects in .htaccess — with regex examples and redirect rules.
Published:
Tags: htaccess redirect rules, 301 redirect htaccess, Apache mod_rewrite
.htaccess Redirect Guide Redirects tell browsers and search engines that a URL has moved. Getting the redirect type and target right determines whether your SEO rankings transfer, partially transfer, or get lost entirely. --- What is Redirect Methods in .htaccess? Apache offers two ways to redirect: directive — simple, good for single URL redirects via modrewrite — powerful, supports regex patterns Use for one-off URL changes. Use for patterns (HTTPS enforcement, www to non-www, subdirectory reorganizations). --- What is Redirect Status Codes? | Code | Name | SEO impact | Use case | |------|------|-----------|---------| | 301 | Permanent | Passes ~99% of link equity | Page moved permanently | | 302 | Temporary | Passes no equity | Temporary unavailability | | 307 | Temporary (HTTP/1.1) |…
Frequently Asked Questions
How do I create a 301 redirect in .htaccess?
Use the `Redirect` directive: `Redirect 301 /old-page/ https://example.com/new-page/`. For pattern-based redirects, use `RewriteEngine On` with `RewriteRule`. The `[R=301,L]` flags at the end of a RewriteRule trigger a 301 redirect and stop processing further rules.
What is mod_rewrite?
mod_rewrite is an Apache module that enables URL rewriting and redirection using regular expressions. It's the engine behind `.htaccess` redirect rules. You must enable it with `RewriteEngine On` at the start of your rule block. Most shared hosts have it enabled by default.
How do I redirect all traffic to HTTPS?
Use: `RewriteEngine On`, `RewriteCond %{HTTPS} off`, `RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]`. This checks if the connection isn't HTTPS and redirects with a 301. Place it before other rewrite rules.
How do I redirect www to non-www?
Use: `RewriteEngine On`, `RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]`, `RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]`. The `%1` captures the domain without `www.` from the condition. Combine with the HTTPS redirect by chaining conditions.
What is the difference between a 301 and 302 redirect?
A 301 is a permanent redirect — search engines transfer ranking signals (link equity) from the old URL to the new one and update their index. A 302 is temporary — search engines keep the original URL indexed and don't transfer equity. Use 301 for permanent changes, 302 only when you genuinely plan to restore the original.
All articles · theproductguy.in