301 Redirect in .htaccess patterns

301 redirects are one of the most common configurations in .htaccess. They permanently redirect visitors and search engines from old URLs to new ones — preserving your SEO ranking and ensuring a smooth user experience during site migrations, URL restructuring, or security upgrades (HTTP → HTTPS).

301 vs 302: What's the Difference?

Before diving into patterns, it's important to understand which redirect type to use:

Use 301 for permanent changes: HTTP→HTTPS, www→non-www, domain migrations, and URL restructuring. Use 302 for temporary holds like A/B testing or maintenance pages.

Always back up your .htaccess file before making changes. A single syntax error can cause a 500 Internal Server Error for your entire site. In DirectAdmin, go to File Manager → public_html/.htaccess → Download a copy first.

Pattern 1: Redirect a Single URL

The simplest redirect — send one specific URL to another. Use this when you rename a page or post.

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

Or using mod_rewrite (more flexible):

RewriteEngine On
RewriteRule ^old-page\.html$ /new-page.html [R=301,L]

Pattern 2: HTTP to HTTPS (Force SSL)

Redirect all HTTP traffic to HTTPS — essential for security and SEO ranking after installing an SSL certificate.

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

This rule checks if HTTPS is off, then rewrites the request to use HTTPS while preserving the full path and query string.

Pattern 3: www to non-www

Redirect www.yourdomain.com to yourdomain.com. This consolidates your domain authority to a single canonical version.

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

The %1 captures the domain without www. from the condition above.

Pattern 4: non-www to www

The opposite — redirect yourdomain.com to www.yourdomain.com. Use only one canonical form consistently.

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

Pattern 5: Redirect an Entire Folder

Redirect all pages inside a folder to a new location. Useful after restructuring site sections.

RewriteEngine On
RewriteRule ^old-blog/(.*)$ /new-blog/$1 [R=301,L]

The (.*) capture group preserves the file path, so /old-blog/post-1.html → /new-blog/post-1.html.

Pattern 6: Full Domain Migration

Redirect all traffic from an old domain to a new one. Use this when you rebrand or move to a new domain.

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

Place this in the .htaccess on olddomain.com. It handles both www and non-www variants.

Pattern 7: Redirect with Query String

Redirect URLs with specific query parameters. Useful after changing from query-string URLs to clean URLs.

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=123$
RewriteRule ^product\.php$ /product/widget-pro? [R=301,L]

The trailing ? in the destination removes the original query string from the redirected URL.

Pattern 8: Combined HTTP + www to HTTPS + non-www

The most complete redirect — handles all four combinations (http/https × www/non-www) and forces the canonical version.

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

Run these two rules together in order. The HTTPS rule fires first, then the www rule, covering all four entry points.

Testing Your Redirects

After adding redirect rules, always verify they work correctly:

Avoid redirect chains. A chain like A → B → C adds latency and dilutes link equity. Always redirect directly to the final destination: A → C.

Common .htaccess Errors

More .htaccess Redirect Patterns

Beyond the eight core patterns above, real-world migrations often need a few more variants. The examples below cover the cases that come up most when tidying up a site's URLs — enforcing a single canonical host, normalizing trailing slashes, and mapping old pages to new ones. Copy them as-is and replace the domain and paths with your own.

Force HTTPS + non-www in One Block (Canonical Host)

If you want every request to end on https://yourdomain.com regardless of how it came in, combine all the conditions into a single rule:

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

The [OR] flag fires the rule when the request is either "not HTTPS" or "has www," so all four entry points (http/https × www/non-www) collapse to one canonical URL in a single redirect — minimizing redirect chains.

Enforce Trailing Slash (Add or Remove)

For URL consistency, pick whether your URLs end with a / and enforce it site-wide. To add a trailing slash to directories:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]

Or the opposite — strip the trailing slash to keep URLs shorter:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

Use only one of these — never both at once, or they will fight each other and cause a redirect loop.

Old Page to New Page (One-to-One)

When you rename an article or restructure a single URL, map it one-to-one so the old page does not lose its ranking:

Redirect 301 /blog/old-article /blog/new-article-slug
Redirect 301 /promo-2026.html /promotions.html

For many pages, use RedirectMatch or list each Redirect 301 line individually so every page points straight to its final destination, never through an intermediate hop.

301 vs 302 vs 307: Which to Use

HTTP has several redirect status codes. The three you will use most are 301, 302, and 307, and each tells browsers and search engines something different. Choosing the wrong one can hurt SEO or break POST requests.

Status Meaning Passes Link Equity Use When
301 Moved Permanently Yes (~90-99%) Migrations, force HTTPS, www/non-www, URL changes
302 Found (Temporary) No Maintenance pages, A/B testing, temporary promos
307 Temporary Redirect (preserves method) No Redirects that must keep POST/method (APIs, forms)

The key difference with 307 is that the browser preserves the original HTTP method (a POST stays a POST), whereas with 302 many browsers downgrade POST to GET. So if you redirect a page that receives form data, consider 307. For ordinary content-site SEO, however, 301 remains the default choice.

301 Redirects and SEO

The 301 redirect is a key tool for preserving rankings when you change URL structure. Done correctly, you keep the authority you have built up; done wrong, you can lose all your Google traffic. Here is what matters for SEO:

After moving a URL, update your sitemap, fix internal links to point directly to the new URL, and submit the new URL to Google Search Console to speed up re-indexing. The 301 then handles old external links you cannot edit.

Frequently Asked Questions

What is the difference between 301 and 302 for SEO?

A 301 is a permanent move: search engines transfer most link equity to the new URL and update their index. A 302 is temporary: search engines keep indexing the original URL and pass no link equity. Use 301 for site migrations, URL changes, and forcing HTTPS. Use 302 only for genuinely temporary holds such as maintenance pages or A/B testing.

How do I fix a redirect loop in .htaccess?

Redirect loops usually happen because the destination still matches the same rule. Add a RewriteCond to exclude the destination from matching — for example check %{HTTPS} off or the %{HTTP_HOST} before rewriting — and never combine an add-trailing-slash rule with a remove-trailing-slash rule. Clear your browser cache, since cached 301s keep redirecting even after you fix the file, and test in incognito mode.

Do 301 redirects affect SEO rankings?

Yes, positively when done right. A 301 transfers roughly 90-99% of link equity from the old URL to the new one and prevents duplicate content when you force a single HTTPS and www or non-www version. Avoid redirect chains (A → B → C) because each hop adds latency and dilutes link equity. Always redirect straight to the final destination.

Does the order of RewriteRules in .htaccess matter?

Yes, it matters a lot. Apache processes rules top to bottom. Broad rules such as forcing HTTPS site-wide should come first, while specific URL rules should use the [L] flag to stop the chain after a match. Wrong ordering can cause some redirects to be skipped or create a loop.

Need Thai Hosting with Full .htaccess Support?

AsiaGB Hosting supports Apache mod_rewrite on all plans — starting at just 500 THB/year with 99% Uptime SLA.

View Hosting Plans