Download the PHP package padosoft/migrate-cloudflare-rules without Composer

On this page you can find all versions of the php package padosoft/migrate-cloudflare-rules. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package migrate-cloudflare-rules

Migrate Cloudflare Rules — Laravel Artisan command

Latest Version on Packagist Tests Total Downloads

padosoft/migrate-cloudflare-rules adds a single Artisan command, cloudflare:migrate, to your Laravel 12+ application. The command copies Cloudflare security configuration from one zone/account (the source) to another zone/account (the destination) using the Cloudflare REST API v4:

It is the tool you want when you move a website to a new Cloudflare account, split a zone into two, clone the security posture of a production zone onto a staging zone, or simply want to keep two zones aligned without clicking through the dashboard for hours.

The command was written for a real migration between two Cloudflare accounts, has been used in production against the live API, and comes with a test-suite that exercises every rule type against a faked Cloudflare API.

Heads-up about the Cloudflare API. Cloudflare has deprecated some of the legacy endpoints this command talks to (Firewall Rules API, the previous-generation Rate Limiting API, and the Page Rules product). Everything is documented in Cloudflare API status and compatibility notes — please read that section before relying on the waf, ratelimit and pagerules types.


Table of contents


Requirements

Requirement Version
PHP 8.2 or newer
Laravel 12.x or 13.x (illuminate/console, illuminate/http, illuminate/support)
Guzzle 7.8+ (pulled in automatically, it is what the Laravel HTTP client uses)
Cloudflare Two API tokens (one for the source, one for the destination) with the permissions listed below

The command runs entirely from the CLI (php artisan …); it does not register routes, views, migrations or middleware.

Installation

Install the package with Composer:

Laravel's package auto-discovery registers the service provider (Padosoft\MigrateCloudflareRules\MigrateCloudflareRulesServiceProvider) automatically. If you have disabled auto-discovery, add it manually to bootstrap/providers.php:

Verify that the command is available:

Configuration

Credentials are never passed on the command line (they would end up in your shell history and be visible to other users through ps). They are read from the package configuration file, which in turn reads them from your .env file.

Environment variables

Add these variables to the .env file of your Laravel application (a ready-to-copy .env.example ships with the package):

Variable Description
CLOUDFLARE_MIGRATE_SOURCE_API_TOKEN API token that can read the source account/zone.
CLOUDFLARE_MIGRATE_SOURCE_ACCOUNT_ID Account ID of the source account (needed by account-level resources).
CLOUDFLARE_MIGRATE_SOURCE_ZONE_ID Zone ID of the source zone (needed by zone-level resources).
CLOUDFLARE_MIGRATE_DESTINATION_API_TOKEN API token that can edit the destination account/zone.
CLOUDFLARE_MIGRATE_DESTINATION_ACCOUNT_ID Account ID of the destination account.
CLOUDFLARE_MIGRATE_DESTINATION_ZONE_ID Zone ID of the destination zone.

Migrating between two zones of the same account? Use the same token and the same account ID on both sides and just change the zone IDs.

If any value required by the selected rule type is missing, the command stops immediately with a message such as:

and exits with code 1, without contacting Cloudflare.

Publishing the config file

Publishing is optional — the package ships with sensible defaults that read the variables above. Publish it only if you want to rename the environment variables or hard-wire values from another source:

This copies the file to config/migrate-cloudflare-rules.php:

Where to find your Account ID and Zone ID

Both identifiers are 32-character hexadecimal strings and are not secrets (they are, however, needed to build the API URLs).

Zone ID

  1. Log in to the Cloudflare dashboard.
  2. Select the account, then click the domain (zone) you are interested in.
  3. On the zone Overview page scroll down the right-hand sidebar to the API box: it shows Zone ID and Account ID, each with a Click to copy link.

Account ID

You need the source values for the zone/account you are copying from and the destination values for the zone/account you are copying to.

How to create the API tokens (and which permissions they need)

Use API tokens, not the legacy Global API Key: tokens can be scoped to the minimum set of permissions and to specific accounts/zones, and can be revoked individually.

  1. Dashboard → click your profile icon (top right) → My Profile → API Tokens (direct link: https://dash.cloudflare.com/profile/api-tokens).
  2. Click Create Token → Create Custom Token → Get started.
  3. Give the token a descriptive name (e.g. migrate-rules SOURCE (read-only)).
  4. Add the permissions from the table below. Use Read for the source token and Edit for the destination token.
  5. Under Account Resources and Zone Resources restrict the token to the specific account and zone (Include → Specific zone → your zone). Never leave it on All zones if you can avoid it.
  6. Optionally restrict Client IP Address Filtering to the IP of the machine running the migration and set a TTL.
  7. Continue to summary → Create Token → copy the token now (it is shown only once) and paste it into your .env.
  8. Repeat for the destination token.

Permissions needed, per rule type (names as displayed in the Cloudflare token editor):

Rule type ({type} argument) Source token (Read) Destination token (Edit)
waf Zone → Firewall Services (legacy Firewall Rules API used to read the source rules and the destination rules for duplicate detection) and Zone → Zone WAF (Rulesets API used to write custom rules) Zone → Zone WAF (Edit), Zone → Firewall Services (Read is enough — used only for duplicate detection)
ipaccessruleszone Zone → Firewall Services Zone → Firewall Services
ipaccessrulesaccount Account → Account Firewall Access Rules Account → Account Firewall Access Rules
useragent Zone → Firewall Services Zone → Firewall Services
ratelimit Zone → Firewall Services Zone → Firewall Services
customlists Account → Account Filter Lists Account → Account Filter Lists
pagerules Zone → Page Rules Zone → Page Rules

If you plan to migrate everything, a convenient minimal pair of tokens is:

You can verify a token before using it:

Which values are needed for which rule type

API tokens are always required. Account IDs and zone IDs are validated depending on the type:

{type} Needs *_ACCOUNT_ID Needs *_ZONE_ID
waf ✔
ipaccessruleszone ✔
ipaccessrulesaccount ✔
useragent ✔
ratelimit ✔
customlists ✔
pagerules ✔

Usage

Arguments

Argument Values Description
type waf, ipaccessrulesaccount, ipaccessruleszone, useragent, ratelimit, customlists, pagerules Which kind of rules to migrate. One type per invocation — run the command once per type.
mode bulk, individual bulk migrates every rule found in the source in one pass, ignoring --exclude / --only_rules_id. individual migrates the rules one by one and honours --exclude and --only_rules_id. Both modes create rules one API call at a time and both skip rules that already exist in the destination — see Duplicate detection. For waf, customlists and pagerules the two modes behave identically: they always iterate rule by rule and always honour the filters.

Options

Option Applies to Description
--exclude=ID individual mode (always for waf, customlists, pagerules) Skip the rule with this source ID. Repeatable: --exclude=a --exclude=b. For customlists the value is the list ID.
--only_rules_id=ID individual mode (always for waf, customlists, pagerules) Migrate only the rules with these source IDs (repeatable). Rules not in the list are reported as "not included in the given list, skipped". For customlists the value is the list name (not the ID).
--source-url=… pagerules (required) Domain (or any substring of the URL) of the source zone, e.g. example.com.
--destination-url=… pagerules (required) Domain (or substring) that replaces --source-url in every Page Rule target, e.g. example.org.
--dryrun all Do not create anything on Cloudflare. Reads from the source (and from the destination for duplicate detection) as usual, then prints the HTTP request that would have been sent for each rule.
--debug all Print every HTTP request and response exchanged with Cloudflare (method, URL, headers, body). The API token is masked.

--exclude and --only_rules_id can be combined; a rule must pass both filters to be migrated.

Recommended workflow

  1. Configure the .env variables and verify the tokens.
  2. Run a dry run first and read the output carefully:

  3. Run the real migration for the same type:

  4. Re-running the same command is safe: rules that already exist in the destination are detected and skipped, so you can run it again after fixing an error and only the missing rules will be created.
  5. Repeat for the other types you need. A typical full migration is:

Examples

Migrate all WAF custom rules:

Migrate WAF rules one by one, skipping two of them:

Migrate only two specific WAF rules:

Migrate the account-level IP Access Rules (e.g. blocked countries / IP ranges shared by all zones):

Migrate the zone-level IP Access Rules, excluding one:

Migrate the User Agent Blocking rules:

Migrate the Rate Limiting rules:

Migrate the Custom Lists (and their IP items). Note that for lists --only_rules_id takes the list name:

Migrate the Page Rules from example.com to example.org — every occurrence of example.com inside the rule targets becomes example.org:

Simulate anything without touching Cloudflare, with the full HTTP conversation printed:

Sample output of a real run:

What the command does, rule type by rule type

Common flow for every type:

  1. Load the credentials from the config and validate them for the selected type.
  2. Download all the existing rules of that type from the destination (paginated, 50 per page). They are used for duplicate detection. (For customlists the destination lists are loaded right before migrating.)
  3. Download all the rules of that type from the source (paginated).
  4. For each source rule: apply --only_rules_id / --exclude (individual mode), check whether an equivalent rule already exists in the destination, otherwise build the payload and POST it to the destination.
  5. Print a line per rule and write errors to the Laravel log (Log::error).

The source rule ID is never reused: it is stripped from the payload, because IDs are unique per account/zone. The destination assigns new IDs.

waf — WAF custom rules

ipaccessrulesaccount / ipaccessruleszone — IP Access Rules

useragent — User Agent Blocking rules

ratelimit — Rate Limiting rules

customlists — Custom Lists

pagerules — Page Rules

Duplicate detection

Before creating anything, the command loads every rule of the selected type from the destination and compares each source rule against them:

Type Considered a duplicate when…
waf same filter.expression or same description
ipaccessrulesaccount, ipaccessruleszone same configuration.target and configuration.value
useragent same description and mode
ratelimit same match.request.url and action
customlists same list name
pagerules never (not implemented)

Duplicates are reported as "Rule id X already exists in the destination account with rule id Y" and skipped. This is what makes the command idempotent for every type except pagerules.

Dry run and debug output

--dryrun performs all the read calls (source rules, destination rules) but replaces every write call with a printout of the request:

--debug additionally prints every request and its response (status, headers, JSON body) — handy to understand a Cloudflare validation error. In both cases the bearer token is masked (only the last 4 characters are shown), so the output can be pasted into a ticket safely.

Everything the command prints goes to STDOUT through the normal Artisan output; errors are also written to the Laravel log channel (Log::error, Log::debug for the raw destination rules).

Error handling, plan limits and exit codes

Cloudflare API status and compatibility notes

The command uses the Cloudflare REST API v4 (https://api.cloudflare.com/client/v4/) — that is the current and only public version, it is not going away. Some of the individual endpoints it calls, however, belong to legacy products that Cloudflare has deprecated after this command was written and battle-tested. Status as of August 2026:

Type Endpoints used Status Modern replacement
waf (read side) GET /zones/{id}/firewall/rules (source rules and destination rules for duplicate detection) Firewall Rules API and Filters API are deprecated and "no longer supported since 2025-06-15" (deprecations, migration guide). Cloudflare had been translating calls to the Rulesets API internally; after the sunset date the endpoint may stop answering at any time. Read the custom rules from the ruleset entry point: GET /zones/{id}/rulesets/phases/http_request_firewall_custom/entrypoint (rules carry expression, action, description, enabled).
waf (write side) GET/POST /zones/{id}/rulesets, POST /zones/{id}/rulesets/{ruleset_id}/rules ✅ Current (Rulesets API). —
ratelimit GET/POST /zones/{id}/rate_limits Rate Limiting API (previous version) is deprecated since 2025-06-15 and the API reference now states that these endpoints return 410 Gone (API reference, upgrade guide). Expect the ratelimit type to fail against the live API. Rulesets API, phase http_ratelimit: GET /zones/{id}/rulesets/phases/http_ratelimit/entrypoint to read, PUT …/entrypoint (or POST /rulesets/{id}/rules) to write rules with expression, action and a ratelimit object (characteristics, period, requests_per_period, mitigation_timeout, counting_expression).
pagerules GET/POST /zones/{id}/pagerules ⚠️ The Page Rules product is deprecated; Cloudflare is auto-migrating existing Page Rules to the new Rules products (2025 onward) and will retire them (migration guide). The API is not on the deprecation list yet and still works, but it will disappear together with the product. Rulesets API phases: http_request_dynamic_redirect (Single Redirects), http_request_cache_settings (Cache Rules), http_config_settings (Configuration Rules), http_request_origin (Origin Rules), http_request_transform (Transform Rules).
useragent GET/POST /zones/{id}/firewall/ua_rules ✅ Supported (not deprecated). Cloudflare recommends custom rules instead, but the endpoint is alive. Optional: WAF custom rules on http.user_agent.
ipaccessruleszone, ipaccessrulesaccount …/firewall/access_rules/rules ✅ Supported (not deprecated). Cloudflare recommends custom rules + lists instead. Optional: WAF custom rules + Lists.
customlists /accounts/{id}/rules/lists, …/items ✅ Current (Lists API). —

What this means in practice

Contributions porting these types to the Rulesets API are very welcome (see Contributing).

Security notes

Troubleshooting

Symptom Cause / fix
Missing Cloudflare configuration: … The .env variables listed in the message are empty. Run php artisan config:clear after editing .env if your config is cached.
Cloudflare API error: … Status Code: 403 … "Authentication error" / code 10000 Wrong token, or the token lacks the permission for that resource / is not scoped to that zone/account. Check the permissions table and verify the token with /user/tokens/verify.
Status Code: 400 … "filter expression is invalid" on waf The expression references a list ($name) or a field not available in the destination (plan feature or list not migrated yet). Migrate customlists first / adjust the expression.
Maximum number of rules reached for the account… and exit code 1 The destination plan cannot hold more rules of that type. Upgrade the plan or buy additional rules, then re-run: existing rules are skipped.
Status Code: 410 on ratelimit The legacy Rate Limiting API has been removed by Cloudflare — see the API notes.
Page Rules created twice Duplicate detection is not implemented for pagerules. Delete the extra rules and use --only_rules_id/--exclude for partial re-runs.
Error while fetching the destination … rules and the command stops The destination read failed (permissions, wrong zone ID, network). Nothing was created.
Nothing is printed for Log::… lines They go to the Laravel log (storage/logs/laravel.log by default), not to the console.

Testing

The package ships with an Orchestra Testbench test-suite that boots a minimal Laravel application, fakes the Cloudflare API with Http::fake() (no network access, stray requests fail the test) and exercises every rule type, dry-run, filters, duplicate detection and configuration validation:

CI runs the suite on PHP 8.2 / 8.3 / 8.4 against Laravel 12 and 13 (see .github/workflows/run-tests.yml).

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Bug reports and pull requests are welcome on GitHub. Ideas that would make great PRs:

Please add tests (see tests/Feature/MigrateCloudflareRulesCommandTest.php for the fake-API helper) and keep the code formatted with composer format (Laravel Pint).

Security vulnerabilities

If you discover a security vulnerability, please email [email protected] instead of using the issue tracker.

Credits

The array_getEx() helper bundled in src/helpers.php is copied from padosoft/support so that this package has no dependency on it.

License

Apache License 2.0. Please see the LICENSE file for more information.


All versions of migrate-cloudflare-rules with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
guzzlehttp/guzzle Version ^7.8|^8.0
illuminate/console Version ^12.0|^13.0
illuminate/http Version ^12.0|^13.0
illuminate/support Version ^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package padosoft/migrate-cloudflare-rules contains the following files

Loading the files please wait ...