1

I have an Amazon ec2 instance with LAMP installed. I edited /etc/apache2/sites-available/000-deault.conf and added this:

<VirtualHost *:80>
        ServerAlias *
        UseCanonicalName Off
        LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
        CustomLog /var/log/apache2/access.log vcommon
        VirtualDocumentRoot /var/www/vhosts/%0
</VirtualHost>

Listen 443
<VirtualHost *:443>
        ServerAlias *
        UseCanonicalName Off
        LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
        CustomLog /var/log/apache2/access.log vcommon
        VirtualDocumentRoot /var/www/vhosts/%0
</VirtualHost>

When I visit my domain names they point to the correct directory and it all works great except for one thing...when I visit the domains with www. it gives this error:

"Not Found

The requested URL / was not found on this server."

My directory structure is /var/www/vhosts/domain.com without the www. - could that be the problem and if so what can I do to fix that without adding both domain.com and www.domain.com directories?

1

You could use this VirtualDocumentRoot, instead:

VirtualDocumentRoot "/var/www/vhosts/%-2.0.%-1.0"

This will make all subdomains (including www.example.com) point to /var/www/vhosts/example.com, too.

BTW, if this is your only/first virtual host (on each port), you shouldn't need ServerAlias * at all; if no other matching virtualhost is found, the default virtualhost gets used anyway.

0

Your configuration has

UseCanonicalName Off

CannonicalName is a type of DNS entry.

servername.com is a DNS type A

www.servername.com is a DNS type CNAME, redirecting to DNS type a servername.com

In the default, UseCanonicalName is disabled, remove the lines from configuration file and reload/restart the apache service with

systemctl restart apache2

Its recommended you configure ServerName and ServerAlias, for return 200 code and web page only if DNS match.

ServerName servername.com

ServerAlias www.servername.com ww2.servername.com


You line Listen 443 is not necessary in this configuration file, place in /etc/apache2/apache2.conf

  • I removed UseCanonicalName Off from both entries and restarted apache. I'm still getting a 404 Not found. You also mentioned returning only if DNS matched, how would I go about doing that? – Dominick Allen Jul 1 '17 at 21:01
  • Remove the line "ServerAlias *" and add "ServerName youdns.com" and "ServerAlias www.youdns.com" in another line. – CorTheZ Jul 1 '17 at 21:12
  • Wouldn't that mean I need a new vhost entry for each domain? – Dominick Allen Jul 1 '17 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.