0

I have a domain name example.com and a VPS with a static IP address 123.123.123.123.
I want the browsers to provide the domain name to access the website content.

In pratice I want people see my website only if they put exemple.com and not 123.123.123.123 on their browser's url bar.

So in my /var/www/ directory I made two subdirectories.
/var/www/default (for the content that those who do not provide the dn will see) and
/var/www/exemple for the actual website content.
I edited my /etc/apache2/sites-enabled/000-default.conf file like this.

<VirtualHost *:80>
    DocumentRoot /var/www/default
</VirtualHost>

<VirtualHost *:80>
    ServerName example.com
    ServerAlias example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/exemple

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

In my mind this should work. In pratice the server gives me the content of /var/www/default both if i put 123.123.123.123 and exemple.com in my browser's url bar. Why?

2
  • You cannot have the ServerName and ServerAlias with the same name. What happens if you delete ServerAlias? Commented Mar 22, 2017 at 15:50
  • You can, it's just pointless. Commented Mar 22, 2017 at 16:15

2 Answers 2

0

Apache will direct traffic to the first matching VirtualHost which it sees in the configuration. Because the first one defined does not define a ServerName or ServerAlias, it's configured to catch any traffic to any bound hostname or any bound IP address on port 80.

2
  • So basically it's not possible to have a different virtual host in the case the browser doesn' give a "Host" header? Commented Mar 22, 2017 at 15:47
  • ServerName 123.123.123.123. Commented Mar 22, 2017 at 15:58
0

You forgot to name the VirtualHost dedicated to example.com. Here are my modifications (with some added suggestions):

<VirtualHost *:80>
    DocumentRoot /var/www/default
</VirtualHost>

<VirtualHost 123.123.123.123:80>                              # <= Should fix your issue
    ServerName example.com
    ServerAlias www.example.com                               # <= Suggested modification

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/exemple

    ErrorLog ${APACHE_LOG_DIR}/error-example.log              # <= Suggested modification
    CustomLog ${APACHE_LOG_DIR}/access-example.log combined   # <= Suggested modification
</VirtualHost>

Of course, don't forget to reload Apache.

1
  • You are right. Fixed. Commented Mar 22, 2017 at 16:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.