Redirect subdomains to corresponding main domain

Roman Random 7 years ago updated 7 years ago 5

I have multiple websites set up with multiple domains and I'm trying to redirect every subdomain to it's main domain, like this:

*.domain.com => domain.com

The problem is that no matter what I do all the subdomains apart from www get redirected to one domain:

w.domain1.com => w.domain1.com
w.domain2.com => w.domain1.com
w.domain3.com => w.domain1.com

Only www stays at the correct domain, but doesn't redirect... except for Wordpress websites:

www.domain1.com => domain1.com (Wordpress)
www.domain2.com => www.domain2.com

I tried setting *.domain.com in the Domains tab, I tried all of the suggested directives in the Content and Advanced tabs' custom configuration fields, and nothing seems to have any effect.

I also have set up a few websites with subdomains of domain1.com that point to different paths and work correctly. How can I catch all the subdomains apart from these and redirect them properly?
Any help would be greatly appreciated!

If I understand you right, you want to have every domain which is not specified elsewhere, to be served by an default domain. To do this, you have to check the "Default" box in your port definition section of the website you want to make default.

Additional Info: The "[::]" part is for IPv6.
Thanks for your response Janxb. Actually what I want to achieve is something different. I will explain using real domains so it will be clearer.

For instance, I have http://romanrandom.com/. If instead I access http://www.romanrandom.com/, it will automatically rewrite the domain removing the www, which is what I want. But if I access http://something.romanrandom.com/ no redirection happens, which is not good, although the page displays. And this is a wordpress site; with other types of site the www subdomain doesn't work either.

And there is another issue. Take http://aguo.audio. If you add any subdomain, http://something.aguo.audio it will happily take you to the site at http://romanrandom.com/ leaving the url unchanged, and that's really bad...

So basically what I want is that all subdomains of my domains are removed from the url, apart from a few that go to different sites. I had this set up with Apache and Lighttpd, but after discovering Ajenti I started fancying the idea of not having to use the command line for everything and installed it with Nginx, which I have no experience with. I could learn how to setup Nginx through it's configuration files, but that would defeat the purpose of my migration to Ajenti. And for SEO purposes I would really like to be able to set the subdomain redirections correctly.

Thanks again for your time!

You are right, the approach i suggested to you will not work if you have multiple domains set up on your server.
But the way you described above has worked for me right now.

You add "*.domain.com" to the domains of the site, you want to catch all subdomains for.
Subdomains you have added to another site will NOT be catched by that wildcard entry (checked that).
Make sure that you click the button "apply changes" at the bottom of that page.
I missed that you want to redirect all subdomains to your main domain.
For doing that, you have to create a new site and add the alias domain i have mentioned above. You will use this site only for redirecting. In this site, you add the following line in the "Custom Settings" box in the "Advanced" section:
rewrite (.*) http://romanrandom.com$1 permanent;
That last method worked. Thank you very much!!
I just wonder why adding a *.domain.com in the same website and a conditional rewrite in the Custom Configuration doesn't work...