Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forwading URL Through Cloudflare API

Tags:

api

cloudflare

I'm trying to create a page rule using Cloudflare's API to forward http to https. Unfortunately, I don't think the documentation is 100% clear on how to do this. Here is the JSON object I'm currently passing to the POST body:

{
  "targets": [
    {
      "target": "url",
      "constraint": {
        "operator": "matches",
        "value": "http://exampletest.com/*"
      }
    }
],
  "actions": [{
    "id": "forwarding_url",
    "value": "https://exampletest.com/$1"
  }]
}

and here is the message I'm getting back:

{
"success": false,
"errors": [
    {
        "code": 1004,
        "message": "Page Rule validation failed: See messages for details."
    }
],
"messages": [
    {
        "code": 1,
        "message": ".settings[0].url: This value should not be blank.",
        "type": null
    },
    {
        "code": 2,
        "message": ".settings[0].statusCode: This value should not be blank.",
        "type": null
    }
],
"result": null

}

So it seems like I need to have a settings object somewhere, but any way I try to add the settings, I'm getting the same message. Does anyone know what I'm doing wrong here? Here is Cloudflare's Documentation on the subject. Not sure if I might be missing something:

https://api.cloudflare.com/#page-rules-for-a-zone-create-page-rule

like image 849
Daniel Kapin Avatar asked Nov 11 '18 22:11

Daniel Kapin


People also ask

How do I redirect a URL to another URL?

Add a new URL redirectClick the URL Redirects tab. In the upper right, click Add URL redirect. In the right panel, select the Standard or Flexible redirect type. A standard redirect is used to redirect one URL to another.

How do I use Cloudflare API?

​​ How to use the APILog in to the Cloudflare dashboard Open external link . Select the user icon on the top right of your dashboard > My Profile. Select API Tokens Open external link > Create Token. You can go to Edit Cloudflare Workers template > Use template or go to Create Custom Token > Get started.


2 Answers

Actually, just figured this one out. It looks like this is the correct format:

{
  "targets": [
    {
      "target": "url",
      "constraint": {
        "operator": "matches",
        "value": "http://exampletest.com/*"
      }
    }
  ],
  "actions": [
    {
      "id": "forwarding_url",
      "value": {
        "url": "https://www.exampletest.com/$1",
        "status_code": 301
      }
    }
  ]
}
like image 185
Daniel Kapin Avatar answered Nov 30 '22 08:11

Daniel Kapin


You have to set a parameter - "status":"active" to actually activate the page rule or else you're just creating an inactive rule.

Cloudflare sets this as an optional parameter, but it is needed to activate the rule.

The updated version of this answer goes like this:

{
  "targets": [
    {
      "target": "url",
      "constraint": {
        "operator": "matches",
        "value": "http://exampletest.com/*"
      }
    }
  ],
  "actions": [
    {
      "id": "forwarding_url",
      "value": {
        "url": "https://www.exampletest.com/$1",
        "status_code": 301
      }
    }
  ],
  "status": "active"
}
like image 26
Verty00 Avatar answered Nov 30 '22 09:11

Verty00