# Domains

> Serve production on your own domain, apex or wildcard, with a Let’s Encrypt certificate the router gets and renews by itself. Domains can also follow a branch or redirect.

- Category: Deploy
- URL: https://www.forkrail.com/docs/domains

## Add a domain

Open the project’s **Domains** page, or use the CLI. `add` prints the DNS records the domain needs.

```bash
forkrail domains add shop.example.com storefront                 # a subdomain
forkrail domains add example.com storefront                      # the apex
forkrail domains add '*.example.com' storefront                  # a wildcard
forkrail domains add www.example.com storefront --redirect example.com --status 308
forkrail domains add staging.example.com storefront --branch staging

forkrail domains ls storefront          # each domain and its certificate
forkrail domains inspect shop.example.com
forkrail domains check shop.example.com # look at DNS again now
forkrail domains update www.example.com --production
forkrail domains rm shop.example.com
```

## DNS records

| Record | For |
| --- | --- |
| `shop.example.com CNAME storefront.<your forkrail domain>` | A subdomain |
| `example.com A <the install’s address>` | The apex (`@`), which can’t be a CNAME |
| `_acme-challenge.shop.example.com CNAME <hash>._acme.<your forkrail domain>` | Optional: the certificate before DNS moves. Required for a wildcard. |

> **On Cloudflare, turn the proxy off:** The record must be **DNS only** (grey cloud). A proxied record shows `invalid (proxied)`, because the router needs to see the visitor and answer the certificate challenge itself.

## Certificates

The router gets each domain a Let’s Encrypt certificate, proving the domain is yours the first way that works:

1. **Cloudflare:** the domain is in a Cloudflare zone your install’s token can edit. The certificate arrives within a minute.
2. **Delegated challenge:** `_acme-challenge.<domain>` is a CNAME to the address `add` printed. This works before the domain points at forkrail, and it’s the only way a wildcard gets a certificate.
3. **HTTP:** the domain already points at forkrail.

Before asking Let’s Encrypt, the router checks the domain’s own nameservers, so a domain whose DNS isn’t ready yet never wastes a validation attempt. It looks again every 30 seconds (every 5 minutes after a day); with the Domains page open it checks every few seconds. Certificates renew with a third of their 90 days left, and you’re emailed if one has under 14 days left.

## Redirect domains and branch domains

- **Redirects:** a domain can redirect (301, 302, 307 or 308) to another of the project’s domains, keeping the path and query. The dashboard offers the `www` pair whenever you add an apex domain.
- **Branches:** a domain with `--branch staging` serves the newest ready preview of that branch, like its branch URL, and moves with each push. It answers 404 until the branch has a ready preview.
- **Wildcards:** `*.example.com` covers one label below the domain.

## Move a domain without downtime

1. **Add the domain**

   On a Cloudflare zone your token edits, the certificate comes within a minute. Elsewhere, add the `_acme-challenge` CNAME it printed first.

```bash
forkrail domains add api.example.com my-api
```

2. **Wait for the certificate**

   Until `domains ls` shows `issued`.

```bash
forkrail domains ls my-api
```

3. **Test it before DNS moves**

   Send a request straight to the router while DNS still points at the old host.

```bash
curl --resolve api.example.com:443:<router address> https://api.example.com/health
```

4. **Lower the TTL**

   Set the record’s TTL to 60 seconds, keeping its target, and wait out the old TTL.

5. **Point the record at forkrail**

   Change the CNAME to `<project>.<your forkrail domain>`. To go back, point it at the old host again.

`forkrail domains rm` stops serving a domain within seconds, so move its DNS away first. Owners and members change domains; developers can ask for a check.
