Redirects from en.xyz.com to xyz.com/en

We are currently using a translation tool that creates subdomains. We will stop using it which means all the pages from subdomains en.xyz.com and fr.xyz.com will have 404. We need to make redirects from all the pages from en.xyz.com and fr.xyz.com to a suitable page (the same one) xyz.com/en and xyz.com/fr. How to bulk match the pages for redirects?

With JavaScript, you will be able to do that.
I don’t know the real scenario, but you can do that on loading.

const englishSubdomain = "en.xyz.com";
const frenchSubdomain = "fr.xyz.com";
const baseUrl = "xyz.com";

function redirectFromSubdomain(url) {
  if (url.startsWith(englishSubdomain)) {
    return url.replace(englishSubdomain, baseUrl + "/en");
  } else if (url.startsWith(frenchSubdomain)) {
    return url.replace(frenchSubdomain, baseUrl + "/fr");
  } else {
    return url; // No redirect for non-subdomain URLs
  }
}

window.addEventListener("load", function() {
  const currentUrl = window.location.href;
  const redirectedUrl = redirectFromSubdomain(currentUrl);
  if (currentUrl !== redirectedUrl) {
    window.location.replace(redirectedUrl);
  }
});

Hello there,

On the website DNS configurations, you can setup a 301 redirect for the subdomains to point to the location you prefer, such as xyz.com/en.