Skip to content
v2.7

Redirects with the CLI

owbase redirects: list, test, import, export and apply the redirect rules of a site, with the JSON file in your repository (redirects as code) and the exit codes for CI.

The owbase redirects group does on the command line what the console does in Redirects: it lists the rules of a site, tests URLs, imports an .htaccess, exports, runs the diagnostics, suggests destinations for 404s and changes the site settings. It is the same API as the console and the AI chat: the rules the CLI saves take effect at the edge immediately, with no new deployment.

The command that changes how you work is apply: the rules live in a JSON file in your repository, reviewed in pull requests like the rest of the code, and CI applies the file to the site (redirects as code).

CLI messages are in English, like the rest of the CLI; API errors arrive as the API returns them.

  • On your machine: the owbase login session (Log in).
  • In CI and scripts: the OWBASE_API_KEY variable, which takes precedence over the session (CI with an API key). Reading, testing and exporting need the sites.read scope; saving (apply --yes, import --yes, suggest --apply-high --yes, config with options) also needs sites.write and rules.read.
  • Project: --project-id <id> or the projectId in the folder’s owbase.json.
  • Site: --site <id>, in every command except list without a site. The ID is in owbase sites list.

Every command accepts --json: the API result goes to stdout, and the text report goes to stderr.

Terminal window
owbase redirects list --site shop
owbase redirects list --site shop --search blog --tag migration
owbase redirects list

With --site, the table shows the rules in the order they run: source → destination, status, type, whether it is enabled, tags and the ID; at the end, the site settings (www, trailing slash, _redirects file) and the plan usage. --search looks in the source, destination, description and tags; --tag shows only the rules with the tag.

Without --site, the list is the project’s: the sites that have rules, how many each one has and the plan usage.

The file is what the JSON export produces, and it can live in the site’s repository:

Terminal window
owbase redirects export --site shop > redirects.json
{
"config": { "www": "to-apex", "trailingSlash": "keep", "redirectsFile": true },
"rules": [
{ "source": "/contact.php", "destination": "/contact", "statusCode": 301 },
{ "type": "placeholder", "source": "/product/{id}", "destination": "/p/{id}", "statusCode": 301 }
]
}

Just the list of rules ([ {...}, {...} ]) also works. The fields of each rule are the API fields; whatever you leave out gets the default. The fields the server fills in ($id, siteId, createdBy, $createdAt, $updatedAt) can stay in the file: the CLI removes them before sending.

To apply:

Terminal window
owbase redirects apply redirects.json --site shop

Each rule in the file becomes an upsert operation of a batch: the rule is matched by its source (type, path and domains; placeholder names do not count). A new rule is created, a changed rule is updated and an equal rule is left as it is, so applying the same file twice changes nothing.

Without --yes, nothing is saved. The command shows the plan: how many rules would be created, updated, kept and deleted, each change marked + (create), ~ (update), - (delete) and ! (problem), the problems of each rule, the plan usage after the change and the diagnostics of the set (loops, chains, rules that never run). To save:

Terminal window
owbase redirects apply redirects.json --site shop --yes

The batch is atomic: either everything goes in, or nothing does.

Terminal window
owbase redirects apply redirects.json --site shop --prune --yes

With --prune, the rules of the site that are not in the file are deleted: the file becomes the complete list. Without it, apply only creates and updates. Check the plan without --yes the first time: everything created in the console that is not in the file shows up with -.

If the file has config, apply also sets the site’s www, trailingSlash and redirectsFile, only where they differ (they show up under Site settings in the plan). Webhooks and codeFile depend on the project and are left out: use config.

.github/workflows/redirects.yml
on:
pull_request:
paths: [redirects.json]
push:
branches: [main]
paths: [redirects.json]
jobs:
redirects:
runs-on: ubuntu-latest
env:
OWBASE_API_KEY: ${{ secrets.OWBASE_API_KEY }}
OWBASE_PROJECT_ID: ${{ vars.OWBASE_PROJECT_ID }}
steps:
- uses: actions/checkout@v4
- run: npm i -g @owbase/cli@1
# On pull requests: only the plan. The step fails if a rule has a problem or goes over the plan.
- run: owbase redirects apply redirects.json --site shop --prune
# On main: save.
- if: github.event_name == 'push'
run: owbase redirects apply redirects.json --site shop --prune --yes

The step exits with code 1 when a rule has a problem or the result goes over the plan limit. In that case nothing is saved, and the report shows the limits and the upgrade address.

Terminal window
owbase redirects import .htaccess --site shop

Converts an .htaccess, nginx, _redirects (Netlify, Cloudflare Pages), CSV or JSON file into rules, like the console import. Without --yes it is only a preview: how many rules came out, how many would be created, updated and kept, the invalid ones with the problem, the lines left out grouped by code, with the reason and up to 5 examples, what became site settings and the rules over the plan.

Option What it does
--format auto|htaccess|nginx|netlify|csv|json File format (default auto: Owbase recognizes it by the content).
--mode merge|replace merge (default) joins with the site’s rules; replace leaves the site with only the file’s rules.
--partial Over the plan limit: imports the first rules that fit, in file order.
--yes Saves.

Importing the same file again duplicates nothing (the same source updates the rule). If the file does not fit the plan and you do not pass --partial, the command exits with 1 and shows the upgrade address.

Terminal window
owbase redirects export --site shop > redirects.json
owbase redirects export --site shop --format htaccess -o .htaccess

--format picks json (default), csv, htaccess, nginx or netlify (what each format carries). Without -o, it goes to stdout; with -o, the CLI does not overwrite an existing file without --yes. The JSON comes out indented, ready to version and for apply.

Terminal window
owbase redirects test /old https://shop.example.com/products/12 --site shop

Follows each URL (or path) through the rules and the www and trailing slash settings of the site, without making a request to the site. The table shows the final URL, the status of the first response (site when no rule matches and the site itself answers), how many hops and the notes: chain (more than one hop: point straight to the final one), loop, external and no rule. --method tests another method (default GET). Up to 500 URLs at a time.

Exits with 1 if any URL falls into a loop.

Terminal window
owbase redirects diagnose --site shop

Checks the set: duplicate rules, rules that never run, loops, chains, destination equal to the source and expired schedules (details). Exits with 1 if there is an error; warnings do not fail.

Terminal window
owbase redirects suggest --site shop
owbase redirects suggest --site shop --days 30

Lists the paths that returned 404, with the suggested destination (from the site’s sitemap, the visited pages and the rule destinations), the reason and the confidence: high, medium or low. Bot scan paths (/wp-login.php, /.env…) show up marked and without a suggestion: better leave them as 404. --days looks at the last days (default 7, up to the plan’s statistics days).

To create the high confidence suggestions as 301 rules:

Terminal window
owbase redirects suggest --site shop --apply-high # shows the plan
owbase redirects suggest --site shop --apply-high --yes # saves
Terminal window
owbase redirects config --site shop
owbase redirects config --site shop --www to-apex --trailing-slash remove

Without options, shows the settings. With options, changes only what was passed:

Option Values
--www off, to-apex (www.shop.example.com → shop.example.com) or to-www (the opposite) — how it works.
--trailing-slash keep, add or remove the slash at the end of paths.
--redirects-file true to keep applying the deployment’s _redirects file after the rules.
--code-file true to read the rules from the deployment’s owbase.redirects.json file.
Code When
0 Success (including the plan without --yes).
1 Error, a rule with a problem or the plan limit in apply and suggest --apply-high, a file that does not fit the plan in import without --partial, a loop in test, an error in diagnose.
Was this page helpful?