5 – Add exceptions with Page Rules
In the Configure HTTPS settings tutorial, you configured zone settings that apply to all incoming requests for example.com. In this tutorial, you will add an exception to these settings using Page Rules.
Specifically, you will increase the security level for a URL known to be expensive to render and cannot be cached: https://www.example.com/expensive-db-call. Additionally, you will add a redirect from the previous URL used to host this page.
Create a new branch and append the configuration.
git checkout -b step5-pagerulePage Rules let you override zone settings for specific URL patterns. Add two Page Rules to your main.tf:
# Increase security for expensive database operationsresource "cloudflare_page_rule" "expensive_endpoint_security" { zone_id = var.zone_id target = "${var.domain}/expensive-db-call" priority = 1
actions = { security_level = "under_attack" }}
# Redirect old URLs to new locationresource "cloudflare_page_rule" "legacy_redirect" { zone_id = var.zone_id target = "${var.domain}/old-location.php" priority = 2
actions = { forwarding_url = { url = "https://www.${var.domain}/expensive-db-call" status_code = 301 } }}The first rule increases security to "Under Attack" mode for your database endpoint. The second rule redirects old URLs with a 301 permanent redirect.
terraform planterraform applyTest the redirect functionality:
curl -I https://example.com/old-location.phpExpected output:
HTTP/1.1 301 Moved PermanentlyLocation: https://example.com/expensive-db-callTest the increased security (Under Attack mode returns a challenge page):
curl -I https://example.com/expensive-db-callExpected output:
HTTP/1.1 503 Service Temporarily UnavailableThe 503 response indicates the Under Attack mode is active, presenting visitors with a challenge page before allowing access to protect against DDoS attacks.
git add main.tfgit commit -m "Step 5 - Add two Page Rules"git pushThe call works as expected. In the first case, the Cloudflare global network responds with a 301 redirecting the browser to the new location. In the second case, the Cloudflare global network initially responds with a 503, which is consistent with the Under Attack mode.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark