Build your own GS1 QR code redirects with Cloudflare
This is how to run GS1 QR redirects on your own domain with Cloudflare, with every step and the upkeep it needs for years. DLCode does all of it in minutes, free for 10 products.
DLCode · Updated · 11 min read
What you build
A GS1 Digital Link QR code holds a web address, such as https://qr.yourbrand.com/01/09506000134352. The 01 means a GTIN follows. A lot can follow too, as in /01/09506000134352/10/LOT123.
When a phone scans the code, your domain must answer with a redirect to the right page. This guide sets that up on Cloudflare. Each step says what you must get right, because the address in a printed code stays the same for as long as the packaging is on shelves.
Every value in this guide is an example. Replace qr.yourbrand.com, the GTINs and the page addresses with yours.
What you need
- A domain you own, with its DNS on Cloudflare. A free Cloudflare account is enough.
- A subdomain for the codes, such as qr.yourbrand.com. Your website and email do not change.
- A GTIN from GS1 for each product. See How to get a GTIN.
- A page for each product, and a recall page for each recalled lot.
- For Option B, a person who can read TypeScript and deploy with the command line.
- A person who owns the Cloudflare account and the domain renewal for as long as the packaging is in use.
Make the QR codes
Any GS1 Digital Link generator works if it lets you set the domain. The free DLCode generator takes a GTIN, your link domain and an optional lot. It makes a label as SVG, PNG, PDF or EPS with the barcode and the QR code side by side, or the QR code alone.
The generator writes the GTIN with 14 digits, such as 09506000134352 for the EAN 9506000134352. Use the same 14-digit form in your redirect rules. If a rule uses the 13-digit form and the code holds 14 digits, the scan matches nothing.
The last digit of a GTIN is a check digit. Check each GTIN before you print. A typo in your rules or on the label sends shoppers to an error page, and you find out only when someone scans.
Decide on the domain before you print. You cannot change it after you print. See Use your own domain for GS1 QR codes.
Option A: Cloudflare Bulk Redirects
Bulk Redirects is a list of fixed source and target addresses. You upload it as a CSV file. There is no code, but every product and every recalled lot is a row you write, upload and test.
1. Add a DNS record so that Cloudflare answers for the subdomain. Cloudflare documents a placeholder for hostnames with no server:
| Type | Name | Content | Proxy status |
|---|---|---|---|
| AAAA | qr | 100:: | Proxied |
2. Make a CSV file with one row per product. The file has no header row. The columns are: source URL, target URL, status code, preserve query string, include subdomains, subpath matching, preserve path suffix.
qr.yourbrand.com/01/09506000134352,https://yourbrand.com/products/oat-milk,307,FALSE,FALSE,TRUE,FALSE
qr.yourbrand.com/01/00012345678905,https://yourbrand.com/products/cold-brew,307,FALSE,FALSE,TRUE,FALSE
qr.yourbrand.com/01/09506000134352/10/LOT123,https://yourbrand.com/recall/oat-milk-lot123,307,FALSE,FALSE,TRUE,FALSEWhy these values:
- 307 is a temporary redirect. The default is 301, which browsers can cache, so a phone might skip your new page after you change a rule.
- Subpath matching TRUE makes a code with a lot, such as /01/09506000134352/10/LOT777, also open the product page.
- Preserve path suffix FALSE stops Cloudflare from adding /10/LOT777 to the end of your product page address.
- Preserve query string FALSE drops anything after ? in the scanned address. Your target pages do not need it.
- Leave out the scheme (https://) in the source. The rule then matches both http and https.
3. In the Cloudflare dashboard, go to Bulk Redirects. Select Create Bulk Redirect List, name it, and import the CSV. Then create a Bulk Redirect Rule that uses the list, and select Save and Deploy.
4. Recall a lot by adding a row for /01/{GTIN}/10/{lot}. When two rows with subpath matching both match a scan, Cloudflare uses the row with the most specific path, so the lot row wins. Other lots still open the product page.
| Limit | Free, Pro, Business |
|---|---|
| Bulk Redirect Rules | 15 |
| Bulk Redirect Lists | 5 |
| Redirects across all lists | 10,000 on Free, 25,000 on Pro, 50,000 on Business |
What Bulk Redirects cannot do: a different page per country, or the same product under an 8, 12 or 13-digit GTIN. Each source is a fixed address. Print only the 14-digit form, or use Option B.
To recall every lot that expires before a date, you find each of those lots yourself and add one row for each. The expiry date is not part of the address that Bulk Redirects matches.
Option B: a small Cloudflare Worker
A Worker is a short program that Cloudflare runs on every scan. This one keeps your products, recalled lots and country pages as lists in the code. Each change to a list is a code change and a new deploy.
1. Make a Worker project. Pick the Hello World example and TypeScript.
npm create cloudflare@latest -- qr-redirects2. Replace src/index.ts with this file, then change the lists to your GTINs and pages.
const PRODUCTS: Record<string, string> = {
"09506000134352": "https://yourbrand.com/products/oat-milk",
"00012345678905": "https://yourbrand.com/products/cold-brew",
"00000096385074": "https://yourbrand.com/products/tea-sampler",
};
const RECALLS: Record<string, string> = {
"09506000134352/LOT123": "https://yourbrand.com/recall/oat-milk-lot123",
};
const COUNTRIES: Record<string, string> = {
"09506000134352/CA": "https://yourbrand.ca/products/oat-milk",
};
const PATH = /^\/01\/(\d{8}|\d{12,14})(?:\/10\/([^/]{1,60}))?\/?$/;
function validCheckDigit(gtin: string): boolean {
let sum = 0;
for (let i = 0; i < 13; i++) sum += Number(gtin[i]) * (i % 2 === 0 ? 3 : 1);
return (10 - (sum % 10)) % 10 === Number(gtin[13]);
}
function redirect(location: string): Response {
return new Response(null, {
status: 307,
headers: { location, "cache-control": "no-store" },
});
}
function notFound(): Response {
return new Response("Not found", { status: 404, headers: { "cache-control": "no-store" } });
}
export default {
async fetch(request: Request): Promise<Response> {
const match = PATH.exec(new URL(request.url).pathname);
if (!match) return notFound();
const gtin = match[1].padStart(14, "0");
if (!validCheckDigit(gtin)) return notFound();
let lot = "";
try {
lot = decodeURIComponent(match[2] ?? "");
} catch {
return notFound();
}
const country = request.cf?.country ?? "";
const target =
(lot && RECALLS[`${gtin}/${lot}`]) || COUNTRIES[`${gtin}/${country}`] || PRODUCTS[gtin];
return target ? redirect(target) : notFound();
},
} satisfies ExportedHandler;What it does on each scan:
- It reads /01/{GTIN} and an optional /10/{lot}. Anything else gets a 404.
- It adds zeros to the left of an 8, 12 or 13-digit GTIN to make 14 digits, so /01/012345678905 and /01/00012345678905 open the same page.
- It checks the check digit. A GTIN with a wrong check digit gets a 404.
- If the GTIN and lot are in RECALLS, it opens the recall page. A recall always wins.
- Otherwise, if the GTIN and the scanner's country are in COUNTRIES, it opens the country page. Cloudflare gives the country as a two-letter code, such as CA or US.
- Otherwise it opens the product page from PRODUCTS. An unknown GTIN gets a 404.
- Every answer is a 307 redirect with cache-control: no-store, so a change applies from the next scan.
3. Attach the Worker to your subdomain. In wrangler.jsonc, add a custom domain. Cloudflare then makes the DNS record and the certificate. The subdomain must not already have a CNAME record.
"routes": [{ "pattern": "qr.yourbrand.com", "custom_domain": true }]4. Deploy it.
npx wrangler deployThe Workers Free plan allows 100,000 requests per day, reset at midnight UTC. Above that, scans get an error page until the reset. If your codes can get more scans than that, use the Workers Paid plan.
What this Worker still does not do:
- Answer ?linkType=linkset with GS1 linkset JSON, which lists every link for a product.
- Publish /.well-known/gs1resolver, the file that describes a GS1 resolver.
- Recall every lot that expires before a date. You would add a list of lots and their expiry dates, and keep it current.
- Count scans. You would add logging and a way to read it.
- Tell you when it fails. You would add a monitor that alerts a person.
Test before you print
Check each code from a terminal. The answer must show 307 and the page you expect.
curl -sI https://qr.yourbrand.com/01/09506000134352- Check one address for each product, and one for each recalled lot.
- Check an address that is not in your rules. It must not open a product page.
- Check a GTIN with a wrong last digit. It must get a 404.
- Print a proof and scan it with an iPhone and an Android phone.
- Scan again after each change to the rules.
Maintenance checklist
Printed codes work only while the domain and the rules stay in place. This work does not end when you deploy. Write it down and give it to someone who will be there in five years.
- Account owner: which person or shared company email owns the Cloudflare account. Turn on two-factor sign-in and add a second admin.
- Domain renewal: keep the domain on auto-renew with a card that will not expire unnoticed. If the domain expires, every printed code stops working.
- DNS: tell your web team that the qr record is printed on packaging and must not be deleted.
- Recall steps: who adds the lot row or the RECALLS entry, how fast, and who tests it. Practice once before you need it.
- New products: add the rule before the packaging ships, and test the address.
- Changed pages: when your website moves a page, update the target in the rules.
- Yearly test: scan one code for each product line and check that it opens the right page.
- Backup: keep a copy of the CSV file or the Worker code outside Cloudflare.
- Code: for Option B, someone must be able to change the Worker and deploy it again for as long as the packaging is in use.
- Monitoring: nothing tells you when scans fail. Check one code address on a schedule and alert a person.
Build it yourself or use DLCode
Here is every step from this guide, next to what it takes in DLCode.
| Step | Build it yourself | DLCode |
|---|---|---|
| GTIN format and check digit | Pad each GTIN to 14 digits and check the last digit. Bulk Redirects does neither. | Accepts GTIN-8, 12, 13 and 14. Checks the check digit on every add, import and scan. |
| Redirect type and caching | Set 307 on every row. In a Worker, also send cache-control: no-store. | Every scan gets a 307 with Cache-Control: no-store. |
| Domain and DNS | Move DNS to Cloudflare, add a record or a custom domain, and keep the domain renewed. | Connect qr.yourbrand.com with one TXT record and one CNAME record. Paid packs include a free domain, with renewals paid through December 31, 2030. |
| Lot and expiry | One row or code entry per lot. Expiry dates are a list you build and keep. | Paid packs put the lot and expiry date in the code and store each lot's record. |
| Recalls | Edit the CSV or the code, deploy, and test, on the day of the recall. | Recall a product, a list of lots, or every lot that expires on or before a date. Each change goes in a recall history. |
| Country pages | Worker only, as a list in the code. | Paid packs limit any link to one country or one language. |
| GS1 linkset and /.well-known/gs1resolver | Not in either option. You write it. | On every plan. |
| Scan counts | Not included. You add logging. | By day, country and device on every plan. No IP addresses or shopper cookies. |
| Monitoring | You set up alerts. | DLCode emails the owner when a connected domain stops working. |
| Print files | Use a separate generator. | SVG, PDF, EPS and PNG, with the barcode and QR code side by side. |
| Upkeep while packaging is on shelves | Your team, for years. | Hosting and unlimited scans included through December 31, 2030. |
Start with 10 products free. No card. When you need more, pay once: $249 for 25 products, $499 for 100, or $1,490 for 1,000. Every pack covers hosting and unlimited scans through December 31, 2030. There is no subscription.
New accounts also get 30 days of the 25-product pack, with lot codes, recalls and country pages, so you can try every step above before you pay.
Sources
- Bulk Redirects · Cloudflare
- Bulk Redirects: URL redirect parameters · Cloudflare
- Bulk Redirects: CSV file format · Cloudflare
- Bulk Redirects: how it works · Cloudflare
- Redirects: availability · Cloudflare
- Workers custom domains · Cloudflare
- Workers limits · Cloudflare
- GS1 Digital Link Standard: URI Syntax · GS1
- GS1-Conformant Resolver Standard · GS1
Get your QR code
Get a print-ready file with your barcode and a GS1 Digital Link QR code side by side. 10 products free, and pack features free for 30 days. No card. Then pay once from $249.