Laravel: Google is indexing my site with subdomains, which are now obsolete. How to redirect?

Situateion:
In my app I had c. 20 subdomains. Now they are gone and https certificates with them.
However, Google is indexing my site with use of the subdomains, which was never the intention. EExample:
Google gets to http://booktitle.domainname.com/shop
and the desired URL for the shop should be https://domainname.com/shop

My solution:

now for some URLs I have created a middleware which processes the referer URL and redirects the user to the correct URL.
Note: There is c. 20 subdomains which are involved.

if(str_contains(url()->current(), 'obsoletesubdomain.domainname') {

    return Redirect::away('https://domainname.com'.request()->getPathInfo());
}

My questions

  1. Is there a better solution to handle the issue?
  2. Where should I put the middleware in Laravel 12 in order to handle the obsolete subdomains sitewide?

Thank you.