Skip to content

Understanding HTTP Redirects - A Complete Guide

Published: at 06:00 PM

Understanding HTTP Redirects: A Complete Guide

HTTP redirects are fundamental to how the web works, yet they’re often misunderstood or misused. In this comprehensive guide, we’ll explore everything you need to know about HTTP redirects, from basic concepts to advanced implementation strategies.

Table of Contents

Open Table of Contents

What Are HTTP Redirects?

An HTTP redirect is a server response that tells a web browser or search engine crawler to go to a different URL instead of the one originally requested. The server returns a special status code (3xx series) along with a Location header that specifies the new URL.

The Redirect Process

Here’s what happens during a typical redirect:

  1. Client Request: A browser requests https://example.com/old-page
  2. Server Response: Server returns status code 301 with Location: https://example.com/new-page
  3. Browser Action: Browser automatically requests the new URL
  4. Final Response: Server returns the actual content (usually status code 200)

This entire process typically happens in milliseconds and is invisible to users.


Why Use Redirects?

Redirects serve multiple crucial purposes:

1. Site Restructuring

When you reorganize your website’s URL structure, redirects ensure users and search engines can still find your content.

Old: /products/category/item-name
New: /shop/item-name

2. Domain Changes

Moving from an old domain to a new one requires redirects to maintain traffic and SEO rankings.

example.com → newbrand.com

3. HTTPS Migration

Ensuring all HTTP traffic goes to HTTPS for security.

http://example.com → https://example.com

4. URL Canonicalization

Forcing a preferred version of your URLs.

example.com → www.example.com

5. A/B Testing

Temporarily redirecting users to test different versions of pages.

6. Tracking and Analytics

Using redirect services to track link clicks and gather analytics.


Types of HTTP Redirects

Let’s examine each redirect type in detail:

301 Moved Permanently

Status Code: 301 Meaning: The resource has permanently moved to a new location

When to Use:

SEO Impact: ✅ POSITIVE

Cache Behavior: Browsers and CDNs may cache aggressively

Example Response:

HTTP/1.1 301 Moved Permanently
Location: https://example.com/new-page
Cache-Control: max-age=31536000

302 Found (Temporary Redirect)

Status Code: 302 Meaning: The resource temporarily lives at a different URI

When to Use:

SEO Impact: ⚠️ NEUTRAL

Cache Behavior: Less aggressive caching

Example Response:

HTTP/1.1 302 Found
Location: https://example.com/temporary-page
Cache-Control: no-cache

307 Temporary Redirect

Status Code: 307 Meaning: Similar to 302 but MUST NOT change HTTP method

When to Use:

Key Difference from 302:

Example: Form submission redirect

POST /form-old → 307 → POST /form-new (preserves POST)

308 Permanent Redirect

Status Code: 308 Meaning: Like 301 but MUST NOT change HTTP method

When to Use:

Benefits:


301 vs 302: The Critical Difference

This is the most important distinction to understand:

Use 301 When:

Use 302 When:

Real-World Example

❌ Wrong:

Old site: example.com
New site: newbrand.com
Redirect: 302 Found
Result: SEO value NOT transferred, old domain stays in search results

✅ Correct:

Old site: example.com
New site: newbrand.com
Redirect: 301 Moved Permanently
Result: SEO value transferred, search engines update indexes

Less Common Redirect Codes

303 See Other

Purpose: Force a GET request after POST (PRG pattern)

Use Case: After form submission, redirect to a results page

POST /submit-form → 303 → GET /thank-you

Benefit: Prevents duplicate submissions on refresh

300 Multiple Choices

Purpose: Multiple redirect options available

Use Case: Rare; server offers choices to client

304 Not Modified

Purpose: Cached version is still valid

Use Case: Resource hasn’t changed since last request


Client-Side Redirects

Meta Refresh

HTML-based redirect using meta tag:

<meta http-equiv="refresh" content="5;url=https://example.com/new-page">

Drawbacks:

When It’s Acceptable:

JavaScript Redirect

// Immediate redirect
window.location.href = 'https://example.com/new-page';

// Or using replace (better for history management)
window.location.replace('https://example.com/new-page');

Drawbacks:

When It’s Acceptable:


Best Practices

1. Always Use Server-Side Redirects

Do: Implement redirects at the server/CDN level

Don’t: Rely on JavaScript or meta refresh for SEO-important redirects

2. Minimize Redirect Chains

Bad:

example.com → www.example.com → www.example.com/home → www.example.com/en/home

Good:

example.com → www.example.com/en/home

Why: Each hop adds latency and risks losing users

3. Use Appropriate Status Codes

Permanent changes: 301 or 308 ✅ Temporary changes: 302 or 307 ❌ “Just to make it work”: No!

4. Set Proper Cache Headers

For 301 (Permanent):

Cache-Control: max-age=31536000, public

For 302 (Temporary):

Cache-Control: no-cache, no-store, must-revalidate

5. Monitor and Update

6. Preserve Query Parameters

Good: example.com/page?utm_source=emailnew.com/page?utm_source=email

Bad: example.com/page?utm_source=emailnew.com/page


Common Mistakes to Avoid

Mistake #1: Using 302 Instead of 301

Problem: Not transferring SEO value during permanent moves

Impact:

Solution: Use 301 for permanent changes

Mistake #2: Redirect Chains

Problem: Multiple redirects in sequence

Impact:

Solution: Redirect directly to final destination

Mistake #3: Redirect Loops

Problem: A redirects to B, B redirects to A

Impact:

Example:

/page-a → /page-b → /page-a (LOOP!)

Solution: Test redirect chains thoroughly

Mistake #4: Redirecting to Homepage

Problem: Redirecting specific pages to homepage

Impact:

Solution: Redirect to relevant replacement content or serve 410 Gone

Mistake #5: Ignoring Mobile

Problem: Not testing redirects on mobile devices

Impact:

Solution: Test with mobile user-agents

Problem: Keeping old URLs in internal links

Impact:

Solution: Update all internal links to point directly to new URLs


Testing Your Redirects

Manual Testing

Use Our Tool: LinkGoWhere

Command Line Testing

# Check redirect with curl
curl -I https://example.com/old-page

# Follow redirects
curl -L -I https://example.com/old-page

# Check specific User-Agent
curl -A "Googlebot" -I https://example.com/old-page

Automated Testing

In CI/CD Pipeline:

// Simple redirect test
const response = await fetch(url, { redirect: 'manual' });
expect(response.status).toBe(301);
expect(response.headers.get('Location')).toBe(expectedUrl);

What to Test

✅ Status code is correct (301 vs 302) ✅ Location header points to correct URL ✅ Query parameters preserved ✅ HTTPS enforced ✅ No redirect chains ✅ Mobile redirects work ✅ Different User-Agents tested


Advanced Redirect Patterns

Conditional Redirects

Redirect based on:

Example (nginx):

if ($http_user_agent ~* mobile) {
    return 301 https://m.example.com$request_uri;
}

Regex Redirects

Pattern-based redirects:

/blog/2024/01/15/post-title → /blog/post-title
/products/category-*/item-* → /shop/item-*

Wildcard Redirects

*.oldsite.com → newsite.com
/old-section/* → /new-section/*

Platform-Specific Implementation

Apache (.htaccess)

# 301 Permanent
Redirect 301 /old-page /new-page

# Entire directory
RedirectMatch 301 ^/old-dir/(.*)$ /new-dir/$1

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

Nginx

# Simple 301
location = /old-page {
    return 301 /new-page;
}

# Regex redirect
location ~ ^/old-dir/(.*)$ {
    return 301 /new-dir/$1;
}

# Force HTTPS
if ($scheme = http) {
    return 301 https://$server_name$request_uri;
}

Cloudflare Pages/Workers

export async function onRequest(context) {
  const url = new URL(context.request.url);

  if (url.pathname === '/old-page') {
    return Response.redirect('https://example.com/new-page', 301);
  }

  return await context.next();
}

Node.js/Express

app.get('/old-page', (req, res) => {
  res.redirect(301, '/new-page');
});

SEO Considerations

Redirect and SEO Value Transfer

Timeline for 301 Redirects:

Best Practices for SEO

  1. Use 301 for permanent moves
  2. Update sitemap immediately
  3. Keep redirects active for minimum 1 year
  4. Update internal links
  5. Monitor search console for errors
  6. Update canonical tags

Conclusion

HTTP redirects are powerful tools when used correctly. Remember:

Need to check your redirects? Use LinkGoWhere to trace complete redirect chains, verify status codes, and ensure your redirects are configured correctly.



Last updated: October 30, 2025