scalpel@labs: ~/glossary/redirect-server-config.mdx5 sections

Redirect Server Config - Write One Rule Instead of Three

Redirect server config is the rule in your web server or CDN that tells it where to send a request. Instead of stacking three rules that each add a hop, you write one rule per entry variant that points directly at the final URL. nginx uses `return 301`, Apache uses `RewriteRule`, and Cloudflare uses Redirect Rules.

extension: Scalpel Redirectsupdated: 2026-08-14read_time: 2 min
less redirect-server-config.mdx

Why it matters

Chains accrete. An http-to-https rule from two years ago, a www rule from last year, and a slug rewrite from last week each fire in turn. Nobody designed it that way; it just happened.

Every extra hop adds latency users pay on every visit. A user in Europe waits a tiny bit longer for each round trip. That's compounded across millions of visits. The fix is always the same: map every entry variant directly at its final destination in a single rule. Get the status code right (301 or 308 for permanent, not 302) and you save users a full round trip.

How it works

The principle is simple: for each entry URL that needs redirecting, write one rule that points straight to the final destination.

Bad: three stacked rules that chain:

  1. http://example.com -> https://example.com (redirect)
  2. https://example.com -> https://www.example.com (redirect)
  3. https://www.example.com/old -> https://www.example.com/new (redirect)

Good: three rules, one per entry variant, each pointing at the final:

  1. http://example.com/* -> https://www.example.com/new
  2. http://www.example.com/* -> https://www.example.com/new
  3. https://example.com/* -> https://www.example.com/new

The final destination is the same for all three; every entry gets there in one hop. Different servers have different syntax for this rule. nginx, Apache, and Cloudflare each require a different approach, but the idea is identical.

What does not matter

You don't need to choose between methods (nginx return vs Apache RewriteRule vs Cloudflare). Each server has its own syntax; pick the one your setup uses. The goal is the same: one rule per entry variant, pointing to the final URL.

You don't need to worry about the browser's HSTS cache interfering with your changes. HSTS policies are long-lived by design, but they only control http to https upgrades on subsequent requests. Your first rule should still fire and be testable.

Code example

nginx

# Bad: stacked rules (each is a separate hop)
server {
  listen 80;
  server_name example.com;
  rewrite ^ https://example.com$request_uri permanent;
}

server {
  listen 443 ssl;
  server_name example.com;
  rewrite ^ https://www.example.com$request_uri permanent;
}

# Good: direct to final destination
server {
  listen 80;
  server_name example.com www.example.com;
  return 301 https://www.example.com$request_uri;
}

server {
  listen 443 ssl;
  server_name example.com;
  return 301 https://www.example.com$request_uri;
}

server {
  listen 443 ssl;
  server_name www.example.com;
  # No redirect needed - this is the final destination
}

Use return instead of rewrite when you just need a redirect. It's faster and clearer.

Apache

# Bad: stacked RewriteRules
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

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

# Good: one rule per entry variant, each to the final
RewriteEngine On

# http://example.com -> https://www.example.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

# http://www.example.com -> https://www.example.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

# https://example.com -> https://www.example.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Remember to escape dots in the domain name. A dot in a regex pattern matches any character; \. matches a literal dot.

Cloudflare

In Cloudflare's Redirect Rules UI, create one rule per entry variant:

  • If hostname equals example.com: Redirect to https://www.example.com
  • If hostname equals www.example.com and scheme is http: Redirect to https://www.example.com

Or use Bulk Redirects to upload a CSV of source and destination pairs, one per line.

How Scalpel Redirects shows it

When the chain matches a collapsible shape (a 301-to-301 chain or an http-to-https-to-www triple), the popup shows a "Suggested fixes" panel. It displays nginx, Apache and Cloudflare snippets tailored to your chain, each with a Copy button. The snippets map every entry variant in your current chain straight to the final URL, so you can paste and deploy. Test the fix with the extension by reloading and verifying the chain is now one hop.

Sources