I am using Apache2 with UBUNTU14 LTS, all "standard" server.
The problem is simple, when using http://www.example.com it works fine, but when using http://example.com it doesn't, it goes to the wrong index.
I have folders at ls /var/www/
example.com example2.example html
wiki.example.com wp.example2.example
Each with different and good index.htm working with http://www.example.com (goes to /var/www/example.com) or http://wiki.example.com, etc. But the index of http://example.com goes to /var/www/html/index.htm, not to /var/www/example.com (!).
At /etc/apache2/sites-available/example.com.conf we have,
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName wiki.example.com
DocumentRoot /var/www/wiki.example.com
</VirtualHost>
EDIT (notes)
We are using a workaround — and please ignore the "real world" examples in the comments, it is not valid as real testing —, an ugly piece of PHP code (not a solution) in the /var/www/html/index.php of the root (no subdomain), to redirect to www subdomain,
if ( preg_match( // if HTTP from other domain, redirects
'/(mydomain1|mydomain2|mydomain3)/',
strtolower($_SERVER['HTTP_HOST']),
$m
) ) switch($m[1]) {
case 'mydomain1':
header("Location: https://www.example.com"); die('');
case 'mydomain2':
header("Location: https://www.example2.example"); die('');
case '...':
header("Location: https://www...."); die('');
}
EDIT
As @covener comment suggest, apachectl -S, results in
VirtualHost configuration:
*:80 is a NameVirtualHost
default server localhost (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost localhost (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost atarica.com.br (/etc/apache2/sites-enabled/atarica.com.br.conf:1)
alias www.atarica.com.br
port 80 namevhost wiki.atarica.com.br (/etc/apache2/sites-enabled/atarica.com.br.conf:13)
port 80 namevhost atarica.com.br (/etc/apache2/sites-enabled/fabrincantes.com.conf:1)
alias www.fabrincantes.com
port 80 namevhost yellowtown.org (/etc/apache2/sites-enabled/yellowtown.org.conf:1)
alias www.yellowtown.org
port 80 namevhost wiki.yellowtown.org (/etc/apache2/sites-enabled/yellowtown.org.conf:35)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
Try this:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
Then you just need to create another section with ServerName www.example.com for your real server configuration.
Also, wouldn't CNAME's records work has an alternative to this? (I'm not sure)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With