Please, I have check multiple threads on stackoverflow but it is a nightmare to understand what is going wrong.
I have setup .../example.com.conf with ServerAlias as *.example.com which i believe would make my canonical name match any subdomain.
Also, I have implemented redirect rule in .htaccess so visit to bar.example.com or foo.example.com would redirect to domain.com for some reason it just doesn't work.
For some obscure reasons I do not know why www.example.com works perfectly. Please do I need to work on the A records? or do something?
Note: Include /etc/httpd/custom.d/mcb.conf I just blocked some bots so and user-agents
I'm providing my exact configure of both files below... I appreciate the help in advance, guiding me what I'm doing wrongly.
<VirtualHost *:80>
ServerName example1.com
ServerAlias *.example1.com
ServerAdmin webmaster@example1.com
DocumentRoot /var/www/example1.com/public_html
<Directory /var/www/example1.com/public_html>
Options +FollowSymLinks
AllowOverride All
Include /etc/httpd/custom.d/mcb.conf
</Directory>
ErrorLog /var/log/httpd/example1.com-error.log
CustomLog /var/log/httpd/example1.com-access.log combined
</VirtualHost>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.example1\.com [NC]
RewriteRule ^(.*)$ http://domain-for-example.com/$1 [R=301,L]
</IfModule>