0

I set up two websites on my vps: one is http site example2.com, the other is https site example1.com:

Listen 80
Listen 443

<virtualhost *:443>
ServerName example1.com
DocumentRoot "/var/www/example1.com"
</virtualhost>

<virtualhost *:80>
ServerName example2.com
DocumentRoot "/var/www/example2.com"
</virtualhost>

Now if I visit http://example1.com, I actually open http://example2.com. How to stop this? I suppose http://example1.com is not accessible. I tried to add a default virtualhost hoping http://example1.com will open the file /var/www/default/index.html, but that did not help. http://example1.com always redirects to http://example2.com:

Listen 80
Listen 443

<virtualhost *>
DocumentRoot "/var/www/default"
</virtualhost>

<virtualhost *:443>
ServerName example1.com
DocumentRoot "/var/www/example1.com"
</virtualhost>

<virtualhost *:80>
ServerName example2.com
DocumentRoot "/var/www/example2.com"
</virtualhost>
1
  • Why did you choose port 443 for example1.com? Have you configured SSL ? Commented Mar 19, 2018 at 8:01

2 Answers 2

0

Now if I visit http://example1.com, I actually open http://example2.com.

That is (in part) because port 80 and port 443 are not automatically interchangeable. There are (generally speaking) no mechanisms to automatically redirect requests from one port to another unless you specify this behavior on the server itself (note that this excludes some browser plugins or features that force HTTPS connections).

I suppose http://example1.com is not accessible[.]

This is correct. Unless you define an HTTP virtual host on port 80 with that domain, it doesn't exist and thus cannot be accessed (forgetting about "default" hosts). I am assuming you have defined example1.com in your "hosts" file or DNS as well.

I tried to add a default virtual host [...] but that did not help.

There could be other issues with your configuration. However, what you most likely need (at least to start) is an actual virtual host entry matching http://example1.com:

Listen 80
Listen 443

# Virtual Hosts For Basic HTTP Services

<VirtualHost *:80>

  ServerName example1.com

  # We can use * to catch all sub-domains other than e.g. www
  # ServerAlias www.example1.com *.example1.com

  ServerAlias www.example1.com

  # DocumentRoot "/var/www/example1.com"
  # DocumentRoot "/var/www/default"

  # If we get any requests on port 80 for example1.com 
  # redirect them to e.g. http://www.example1.com:443/

  Redirect permanent / http://www.example1.com:443/

</VirtualHost>

# This is not an SSL (HTTPS) virtual host

<VirtualHost *:443>
  ServerName example1.com
  DocumentRoot "/var/www/example1.com"
</VirtualHost>

# Our other domain

<VirtualHost *:80>
  ServerName example2.com
  DocumentRoot "/var/www/example2.com"
</VirtualHost>
0

When you use http://, you connect to port 80. On port 80 you only have example2.com, which handles al requests for port 80. Try using:

https://example1.com

https, by default, connects to port 443. You can override the defaults, by specifying the port.

http://example1.com:443/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .