Aliif Arief Homepage

How to Redirect Cloudflare Pages Domain to Custom Domain

By Aliif Arief · 1 minute · 143 Words

Hello folks,

You should know that until now, when this article was written, Cloudflare pages did not support redirecting the default domain to a custom domain that we have via the HTTP header. Hence, we have to overcome it by using JavaScript to redirect.

The following is the JavaScript code to redirect a domain to a domain without a dynamic relative path.

if (window.location.hostname !== "aliif.space"){
    window.top.location.href = 'https://aliif.space';
}

And below is the JavaScript code to redirect the domain to the domain with a dynamic relative path.

if (window.location.hostname !== "aliif.space"){
   	window.top.location.href = 'https://aliif.space' + window.location.pathname;
}

Replace the aliif.space domain with your custom domain, then wrap one of the scripts with <script></script> tags and place them before the closing </head> tag on your web page.

Thank you for reading. If there are errors or questions, don’t hesitate to leave a comment.

----

Published Aug 7, 2022

Normalisasi htdocs XAMPP di Ubuntu 6:32