Redirection workaround using DNS zone edit + JS

I have a website with domain X (X is a placeholder, obviously) and recently purchased a new domain Y which I pointed to X via DNS zone editing. I actually wanted domain Y to go to a specific page at X (e.g. Y. com would go to X. com/page instead of just X. com), but I can’t do this unless I additionally purchased hosting for Y so I can edit the htaccess for Y specifically, and I didn’t want to purchase additional hosting if not needed. I alternatively set up a redirect using javascript on the index.html of X. com:

<script>
 if (window.location.hostname=="Y. com") {
  window.location.href = 'X. com/page';
 }
</script>

I just wanted to check in with more experienced devs whether the above DNS zone edit with JS redirect workaround is okay and not too harmful SEO-wise / etc. And/or if you have alternative suggestions on a setup that also doesn’t require separate hosting for Y. Thank you.

P.S. I guess those who disable JS won’t be redirected - but I’m guessing they’re a minority and it’s not a critical, super-important redirect, so it’s fine.

It would probably be better to have the server of X check for referals from Y and direct those to the specific page.

1 Like

Just set up a free redirect for X at Cloudflare.