DNS records

  • List DNS records of a zone:

    1 $ curl -X GET "https://api.cloudflare.com/client/v4/zones/<ZONE_ID>/dns_records" \
    2     -H "Authorization: Bearer <TOKEN>" \
    3     -H "Content-Type:application/json"
  • List DNS records IDs of a zone:

    1 $ curl -X GET "https://api.cloudflare.com/client/v4/zones/<ZONE_ID>/dns_records" \
    2     -H "Authorization: Bearer <TOKEN>" \
    3     -H "Content-Type:application/json" \
    4     | jq '.result[].id'
    5 "82c881261189dc8b8ddbd756cffccd21"
    6 "324437ed3e1212770edeabb65bb3cd6a"
  • Delete one DNS record:

    1 $ curl -X DELETE "https://api.cloudflare.com/client/v4/zones/<ZONE_ID>/dns_records/<RECORD_ID>" \
    2     -H "Authorization: Bearer <TOKEN>" \
    3     -H "Content-Type:application/json"
  • Delete all DNS records of a zone:

    1 $ curl -X GET "https://api.cloudflare.com/client/v4/zones/<ZONE_ID>/dns_records" \
    2     -H "Authorization: Bearer <TOKEN>" \
    3     -H "Content-Type:application/json" \
    4     | jq --raw-output '.result[].id' \
    5     | xargs -I '{}' \
    6     curl -X DELETE "https://api.cloudflare.com/client/v4/zones/<ZONE_ID>/dns_records/{}" \
    7         -H "Authorization: Bearer <TOKEN>" \
    8         -H "Content-Type:application/json"

Redirects

CloudFlare have several redirection options:

Pages redirects

This blog uses CloudFlare Pages to host all its static content. To redirect old URLs to new ones, I use a _redirects file at the root of the repository. You should inspect that file to see how it works.

Note that this kind of redirects are not working at the domain-level. If you want to redirect https://example.com/ to https://www.example.com/, you need to use Page Rules.

Here are some of the most useful rules:

  • By default, pages are redirected for normalization:

    From

    To

    /contact

    /contact

    /contact/

    /contact

    /contact.html

    /contact

    /about

    /about/

    /about/

    /about/

    /about/index.html

    /about/

    Note how folder roots with an index.html are always redirected to an URL with a trailing slash, while HTML files are stripped of their extension.

    This behavior cannot be changed.

  • Redirect a single URL:

    /old-url /new-url 301
    
  • Redirect empty folders to the root of the site:

    /empty-folder      /   301
    /empty-folder/     /   301
    

    I use this double rule to catch both the folder named empty-folder, and an hypothetical file named empty-folder.html. That way I am sure a parasitic empty-folder.html won’t be served.

Page rules

TODO