Redirect xxx.pages.dev To The Custom Domain

The only way i know of is a local script:

<script>
    if (window.location.hostname !== "www.mydomain.com"){
        window.top.location.href = 'https://www.mydomain.com';
    }
</script>

Does Cloudflare have a server-side option to redirect xxx.pages.dev to the custom domain created?

There’s a few options:

The easiest solution is to use Bulk Redirects. You can setup bulk redirects and add a redirect from your pages.dev zone to your real domain very easily in the Cloudflare dashboard.


Or you could do something like this in your _headers:

https://:project.pages.dev/*
  Link: <https://example.com/:splat>; rel="canonical"
  X-Robots-Tag: noindex
  Refresh: 5; url=https://example.com/:splat

This instructs search engines that the real URL is somewhere else (via canonical), not to index the current page, and then uses the Refresh header to instruct clients to redirect after 5 seconds to a new URL.

Do note however that Refresh isn’t actually part of the HTTP standard, but is supported in all common browsers. I’d probably do a combination of all of the above, plus your client-side script, if you were to use this option.


And finally another alternative would be to use a Pages Function specifically and return a 301 response when request.url matches pages.dev.

2 Likes

Bulk redirects worked for me, thank you!

1 Like

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