Direct Answer
RedirHub has an official Terraform provider, redirhub/redirhub on the Terraform Registry. Every link is one redirhub_link resource: domain redirects, website migration mappings, branded short links and QR codes. Domains are redirhub_domain, with the DNS records they need. Review changes as a plan in a pull request and apply them from CI with an API token. It works on every plan that includes the API: Core, Pro and Enterprise.
1. Set up the provider#
Create a token as described in Create and manage API tokens. The token belongs to one workspace, and Terraform manages that workspace. Pass it in the REDIRHUB_API_TOKEN environment variable rather than writing it in your files.
hclterraform {
required_providers {
redirhub = {
source = "redirhub/redirhub"
version = "~> 0.1"
}
}
}
# Reads the token from REDIRHUB_API_TOKEN.
provider "redirhub" {}bashexport REDIRHUB_API_TOKEN="your-token"
terraform init2. Add redirects, short links and QR codes#
handler says what the link is: redirect, migration, short-url or qr. Redirects and migration mappings take url, the source they answer for. Short links and QR codes take host, one of your short-link domains, and an optional alias. The other attributes are the same fields as in the API: destinations, type, forward_path, forward_query, status, tags and utm.
hcl# Send a whole domain to its new home, path and query included.
resource "redirhub_link" "old_domain" {
handler = "redirect"
url = "old-site.com"
destinations = ["https://new-site.com"]
type = "301"
forward_path = true
forward_query = true
}
# A website migration: one mapping per old page, kept in a CSV next to the config.
resource "redirhub_link" "migration" {
for_each = { for row in csvdecode(file("${path.module}/redirects.csv")) : row.from => row }
handler = "migration"
url = each.value.from
destinations = [each.value.to]
type = "301"
}
# A branded short link and a dynamic QR code.
resource "redirhub_link" "spring" {
handler = "short-url"
host = "go.example.com"
alias = "spring"
destinations = ["https://example.com/campaigns/spring"]
utm = { source = "newsletter", campaign = "spring" }
}
resource "redirhub_link" "menu" {
handler = "qr"
host = "go.example.com"
alias = "menu"
destinations = ["https://example.com/menu"]
qr = { theme = "dark", logo = true }
}Run terraform plan to see what will change and terraform apply to make the change. Terraform checks each link's fields before it sends anything, so a redirect without url or a QR code without host fails in the plan. A plan limit, such as tags or several destinations on a plan that doesn't include them, comes back with the same message as in the dashboard.
3. Connect a domain and its DNS in the same apply#
redirhub_domain connects a domain and returns the DNS records it needs in dns_records. Hand them to your DNS provider's Terraform resource. redirhub_domain_verification then waits until the records are live, so the links that use the domain go out only once it answers. It waits 10 minutes by default; raise timeout if your DNS is slow.
hclresource "redirhub_domain" "go" {
host = "go.example.com"
short_links = true
}
# Here with Cloudflare; any DNS provider with a Terraform record resource works the same way.
resource "cloudflare_dns_record" "redirhub" {
for_each = { for r in redirhub_domain.go.dns_records : "${r.type}-${r.name}" => r }
zone_id = var.cloudflare_zone_id
name = each.value.name
type = each.value.type
content = each.value.value
ttl = 300
}
resource "redirhub_domain_verification" "go" {
host = redirhub_domain.go.host
depends_on = [cloudflare_dns_record.redirhub]
}
resource "redirhub_link" "launch" {
handler = "short-url"
host = redirhub_domain_verification.go.host
alias = "launch"
destinations = ["https://example.com/launch"]
}4. Bring existing links under Terraform#
Links you made in the dashboard keep working. To manage one with Terraform, import it by its id, for example link_7bXmR4, which the dashboard and the API show. Import a domain by its hostname. The redirhub_links data source lists ids with the same filters as the API, such as handler, host and tags.
hcldata "redirhub_links" "old_site" {
handler = "redirect,migration"
host = "old-site.com"
}
output "old_site_ids" {
value = { for l in data.redirhub_links.old_site.links : l.url => l.id }
}
# Terraform 1.5 and later: import in the plan, then manage it as usual.
import {
to = redirhub_link.old_domain
id = "link_7bXmR4"
}A change someone makes in the dashboard afterwards shows up in the next plan, and terraform apply sets it back to what your files say. A link removed from your files is deleted on the next apply.
5. Plan in pull requests, apply on merge#
Keep the configuration in a folder of your repository and the Terraform state in a remote backend such as S3 or HCP Terraform. Store the token as a CI secret. This GitHub Actions workflow posts the plan on every pull request and applies it when the pull request is merged. A GitLab CI pipeline is in the provider's examples.
yaml# .github/workflows/redirects.yml
name: Redirects
on:
pull_request:
paths: ['redirects/**']
push:
branches: [main]
paths: ['redirects/**']
permissions:
contents: read
pull-requests: write
concurrency: redirects
jobs:
terraform:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: redirects
env:
REDIRHUB_API_TOKEN: ${{ secrets.REDIRHUB_API_TOKEN }}
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
with:
terraform_wrapper: false
- run: terraform init -input=false
- run: terraform plan -input=false -no-color -out=tfplan | tee plan.txt
- if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const plan = require('fs').readFileSync('redirects/plan.txt', 'utf8');
await github.rest.issues.createComment({ ...context.repo, issue_number: context.issue.number, body: '```\n' + plan.slice(-60000) + '\n```' });
- if: github.event_name == 'push'
run: terraform apply -input=false tfplanSafe to run again#
CI jobs time out and get retried. Every link the provider creates carries an Idempotency-Key, so when a response is lost on the way back, the retry gets the link RedirHub already made instead of a second copy. Rate limits and temporary server errors are retried by themselves. Your own scripts can do the same: send any unique value in the Idempotency-Key header of a POST, and the same key within 24 hours returns the first response, marked Idempotent-Replayed: true.
bashcurl -X POST "https://api.redirhub.com/v1/links" \
-H "Authorization: Bearer $REDIRHUB_API_TOKEN" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{"handler": "redirect", "url": "old-site.com", "destination": "https://new-site.com"}'Every attribute is listed in the provider documentation on the Terraform Registry. To script one-off jobs without Terraform, see Create, update and delete redirects in bulk with the API.
Frequently asked questions
Yes. The official provider is redirhub/redirhub on the Terraform Registry. It manages redirects, website migration mappings, branded short links, QR codes and domains.
Every plan that includes the API: Core, Pro and Enterprise. Plan limits such as tags or several destinations apply as they do in the dashboard.
Yes. Import each link by its id, or each domain by its hostname. The redirhub_links data source lists the ids of the links on a domain or with a tag.
Nothing is created twice. A second apply with no changes in your files changes nothing, and a retried create carries the same Idempotency-Key, so RedirHub returns the link it already made.
OpenTofu can use the provider with source = "registry.terraform.io/redirhub/redirhub". For Pulumi and other tools, use the API: it is plain JSON over HTTPS with a Bearer token.
