How can i forward url with 200 code?

Hello, how can I forward url (using page rules) with 200 status code?

Are you talking forward as in redirect?

There is no way to forward with a 2XX code with Page Rules (or at all really, most browsers won’t listen), though you can redirect using JavaScript on the client side by returning a workers response.

What use case do you have to redirect with a 2XX code as opposed to a 3XX code?

2 Likes

+1

2XX Codes = “Successful responses”
3XX Codes = “Redirection messages”
4XX Codes = “Client error responses”
5XX Codes = “Server error responses”

You can have a read here:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

But if you anyway want to redirect AFTER a 200 Code and without PageRules but natively you can use JavaScript to do so after the page is loaded. To execute faster I would recommend inlineJS, something like this:

<script type="text/javascript">
    location = "https://domain.tld";
</script>

or with location.href instead of location (its the very same!)

<script type="text/javascript">
    location.href = "https://domain.tld";
</script>

Have fun!

1 Like

Yes I mean redirect, I want to redirect *.mydomain.com/script.js (for all subdomains) to mydomain.com/script.js with 2XX status code
How is this possible?

Redirection are just possible with 3XX Status Code.

But what you can do:

  1. change the LINKS in the DOM so they initialy do not begin with *.mydomain.com but with mydomain.com
  2. Set up the PageRule anyway but its not possible with 2XX Code.
    The PageRule would look like:

Option 1 (just for script.js)

Pattern: *.mydomain.com/script.js
Target: https://mydomain.com/script.js

Option 2 (for ALL JS-Files)

Pattern: *.mydomain.com/*.js
Target: https://mydomain.com/$2.js

1 Like

Is it possible to redirect the url with 200 status code using cloudflare workers?

It is not possible to redirect with a 200 Code at all, with no technic in this world, as 200 Codes is NO REDIRECT!

2 Likes

I can do this with node.js and express - res.sendStatus(200).redirect("url")
but I want to make it for all subdomains using cloudflare

Please show me a example where this kind of redirect is applied I would like to inspect it.

1 Like

And the documentation is relatively clear that this will send a 302

Express 4.x - API Reference

We are really going a bit in circles I am afraid. As @jb3 and @M4rt1n wrote several times, what you are asking is neither possible nor does it make sense (a redirect is a redirect and content is content).

Yes, you can include the URL in a 200 response, but no remotely sane HTTP client will follow that. Unless you prefer more “feminine” servers

3 Likes

You can’t do that, you need a 3xx redirection code. For a list of 3xx redirection codes visit 3xx Redirection – Cloudflare Help Center

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.