Redirect all non-www subdomains to main www domain with Apache2
I'm no Apache expert, but have considered changing:
ServerName www.domain.com
ServerAlias domain.com domain2.com domain3.com *.domain.com *.domain2.com *.domain3.com
edit: On rereading the OP, it looks like you're already configuring www.domain.com in a different file, which I don't believe is allowed. That, however, may be part of the problem. If I'm following you correctly, the www.domain.com response is from that file, not the config posted here.
Mike Holdsworth
(Last updated 2018-08-28.) http://stackoverflow.com/questions/1825585/how-to-determine-what-version-of-powershell-is-installed/1825807#1825807 Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (7 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly. My other accounts: iRosetta. [/]. Stack Overflow (SO). [/]. Server Fault (SF). [/]. Super User (SU). [/]. Meta Stack Overflow (MSO). [/]. Careers. [/]. Other My 15 minutes of fame on Super User Blog. Sample: Jump the shark. LinkedIn profile @PeterMortensen (Twitter) Google profile Quora profile GitHub profile Full jump page with other SOFU related, Stack Exchange sites, etc. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com).
Updated on September 17, 2022Comments
-
Mike Holdsworth over 1 year
I have a website (domain.com) and I would like to redirect all my secondary domains (domain2.com, domain3.com) and all the subdomains of these domains (*.domain.com, *.domain2.com...) to the main domain, www.domain.com (because I want the latter to be the only URL to gain access to the site).
For this purpose, I have created an Apache Virtual Host to catch all these possibilities and redirect them (after having configured my DNS, that goes without saying). I put this configuration in a file named "999-catchall" in the folder "sites-enabled" of Apache. NB : I use this name to be sure that it will be the last vhost checked, because I have also my default vhost (000-default for www.domain.com) and a vhost for my webmail (001-webmail for webmail.domain.com).
Here is the content of this "999-catchall" file:
<VirtualHost *:80> # catch... ServerName domain.com ServerAlias domain2.com domain3.com *.domain.com *.domain2.com *.domain3.com # ...and redirect RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L] </VirtualHost>
This configuration works for domain.com, domain2.com, *.domain2.com, domain3.com and *.domain3.com but not for *.domain.com. Example : If I type blabla.domain2.com, I'm redirected to www.domain.com, but if I type blaba.domain.com, I'm not (I just have a "Server not found" error).
Is my method correct? Do you see where my mistake is?
EDIT : My mistake, my DNS server wasn't configured properly for *.domain.com. So this configuration works, if it can help someone who would want to do the same thing.
-
Admin over 14 yearsThat's what I thought too, but if it was the case (if blabla.domain.com was caught by another vhost), it should render something and not "Server not found", no?
-
TKguru42 over 14 yearsYes, I'm configuring www.domain.com in another file (000-default). Why isn't it allowed? and what is the good pratice? I'm a bit lost...
-
David over 14 yearswget result : Resolving blabla.domain.com... failed:: No address associated with nodename. wget: unable to resolve host address `blabla.domain.com'
-
David over 14 years... OK this result made me realize that it could be a DNS misconfiguration. Actually, it is the case. I hadn't specified a DNS entry for *.domain.com! My "after having configured my DNS, that goes without saying" was a bit presomptuous! I will wait for the DNS to propagate before celebrating, but it seems that the error comes from this. Sorry for the disturbing.