0

Is it possible in the apache config file to use regular expressions in the ServerName and ServerAlias field to have 2 domains use the exact same config file vs. creating 2 for each domain. I was trying something like this, but was getting an error from Apache:

<VirtualHost *:80>
    ServerAdmin admin@test.com
    ServerName [test.com|test.net]
    ServerAlias [www.test.com|www.test.net]
    DocumentRoot /var/www/test.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
ControlAltDelete
  • 3,576
  • 5
  • 32
  • 50
  • Refer https://stackoverflow.com/questions/11169843/apache-virtual-host-definition-with-regex – Pandurang Jun 28 '20 at 14:09
  • Yes I saw the wild card at the beginning for subdomains, but am wondering what the syntax would be and if it is possible to have one config file for two domains like test.com and test.net and more possibly. – ControlAltDelete Jun 28 '20 at 15:10
  • I will suggest you to use wildcard for sudomain. You can refer https://stackoverflow.com/questions/758351/virtualhost-for-wildcard-subdomain-and-static-subdomain – Pandurang Jun 28 '20 at 15:24
  • They are not subdomains one is a .com and another is a .net – ControlAltDelete Jun 28 '20 at 16:13
  • I do not think Apache does that. One trick could be to put that `` as the first in the configuration, remove `ServerName` and `ServerAlias` and use it as the default. When Apache cannot match the name, it uses the first `` if finds from the top. But that means that any connection to reach your port 80 would match that one as well. For that type of config, I never combine two sites together (in company servers), since I want my logs to be separated for each site. And sites come and go, so it is easier when they are not coupled. YMMV – Nic3500 Jun 29 '20 at 05:19

1 Answers1

0

I have a similar situation and I ended up with something like this (in your terms):

<VirtualHost test.net:80>
    ServerAdmin admin@test.com
    ServerName test.net
    ServerAlias test.com www.test.net www.test.com
    DocumentRoot /var/www/test.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost> 
Sergey Shevchuk
  • 151
  • 1
  • 5