Back to lessons

Hosting Operations

Find Web Server Redirect Rules

You need to find redirect-related rules across Nginx and Apache configs.

Command

grep -RInE 'return[[:space:]]+30[18]|rewrite[[:space:]]|Redirect[[:space:]]|RewriteRule|RewriteCond' fixtures/nginx fixtures/apache

What changed

Nothing changes. The command searches for common redirect directives.

Danger

safe

When to use it

Use when browsers report too many redirects or HTTP and HTTPS behavior disagrees.

When not to use it

Do not assume every rewrite is a redirect; inspect status codes and conditions before editing.

Undo or recovery

No undo needed because this command is read-only.

Expected output

Config paths and redirect or rewrite lines.

demo script

Disposable terminal steps

  1. grep -RInE 'return[[:space:]]+30[18]|rewrite[[:space:]]|Redirect[[:space:]]|RewriteRule|RewriteCond' fixtures/nginx fixtures/apache
  2. grep -RInE 'server_name|ServerName' fixtures/nginx fixtures/apache

simulated output

What it looks like

disposable vessel
::fixture-ready::
$ grep -RInE 'return[[:space:]]+30[18]|rewrite[[:space:]]|Redirect[[:space:]]|RewriteRule|RewriteCond' fixtures/nginx fixtures/apache
fixtures/nginx/sites-enabled/example.conf:4:    return 301 https://example.com$request_uri;
fixtures/apache/sites-enabled/example.conf:5:    Redirect permanent / https://example.com/
fixtures/apache/sites-available/example.conf:5:    Redirect permanent / https://example.com/
::exit-code::0
$ grep -RInE 'server_name|ServerName' fixtures/nginx fixtures/apache
fixtures/nginx/sites-enabled/example.conf:3:    server_name example.com www.example.com;
fixtures/nginx/sites-enabled/example.conf:8:    server_name example.com www.example.com;
fixtures/nginx/conf.d/admin.conf:3:    server_name admin.internal;
fixtures/apache/sites-enabled/app.conf:2:    ServerName app.example.com
fixtures/apache/sites-enabled/example.conf:2:    ServerName legacy.example.com
fixtures/apache/sites-available/app.conf:2:    ServerName app.example.com
fixtures/apache/sites-available/example.conf:2:    ServerName legacy.example.com
::exit-code::0

YouTube Short

Find redirect rules fast.

Redirect loops often come from one small rule. Search Nginx and Apache config before poking at DNS or certificates.

LinkedIn hook

The redirect loop was hiding in plain text.

Question: What is your first command for a redirect loop?

experiments

A/B tests to run

Metric: save_rate

A: Redirect loop in config.

B: Find redirect rules before guessing.