Packages

Elixir library and Mix task that checks a CSV redirect map for self redirects, duplicates, conflicting targets, redirect chains and loops. It never fetches a URL.

Current section

Files

Jump to
Raw

README.md

# redirect_map_check

Elixir library and Mix task. Give it a CSV redirect map (from_path, to_path). It reports self redirects, duplicates, conflicts, chains and loops, shows the first 10 of each kind, and ends with a summary line.

Both cells are trimmed and then compared exactly as strings: case and a trailing slash are significant, so `/Pricing`, `/pricing` and `/pricing/` are three different paths. A self redirect is a row whose from and to are equal; it is reported on its own and left out of the chain and loop checks. A duplicate is a source listed more than once with the same target. A conflict is a source listed with different targets; the target of its first row is the one used for the chain and loop checks. A chain is a source whose target is itself a source: one line per chain start (a source no other source points at), with the number of hops and the final target, and the advice to point the start straight at the final target. A chain that runs into a loop is shown as ending in a loop. A loop is a cycle among two or more sources, listed once, written `A > B > A` and starting at its smallest path. Walks use a visited set and stop after 100 hops, so long chains and loops never recurse without end.

The task prints the report and exits 0. With `--strict` it exits 1 when any self redirect, conflict, chain or loop exists; duplicates alone are only reported. It exits 2 when the input cannot be read: a missing file, a missing column, no rows, an empty from or to cell (the message names the row) or an unknown option. It never fetches a URL and cannot know a final status code: it only reads the map you give it.

Part of the [OneGTMLab](https://www.onegtmlab.dev) free GTM tools. Background on the idea: [DataForSEO](https://www.onegtmlab.dev/tool-directory/research-and-scraping/dataforseo).

## Install

```
{:redirect_map_check, "~> 1.0"}   # in mix.exs deps
```

## Use

CSV columns: from_path, to_path. The sample has a self redirect, a duplicate, a conflict, two chains and a loop. Add --strict to fail on problems.

```
$ mix redirect_map_check sample.csv
rows: 13
distinct sources: 11
self redirects: 1
  /promo > /promo
duplicates (same source, same target): 1
  /about-us > /about (2 rows)
conflicts (same source, different targets; first row's target used for chains and loops): 1
  /team > /about | /people
chains: 2
  /blog/alpha > /blog/beta > /blog/gamma > /blog/delta (3 hops): point /blog/alpha straight at /blog/delta
  /old-pricing > /pricing-2024 > /pricing (2 hops): point /old-pricing straight at /pricing
loops: 1
  /loop-a > /loop-b > /loop-a
summary: 13 rows, 1 self redirects, 1 duplicates, 1 conflicts, 2 chains, 1 loops
```

## Test

```
mix test
```

Offline, no network calls, no dependencies beyond the language runtime. MIT licensed.