Apache2 virtual host redirection issue on Chrome

chromehostvirtualhostvirtualization

I am having an extremely bizarre issue that seems only present on Chrome, IE and Firefox are fine. I have 2 website being served by 1 IP address, I have 2 identical files in sites-available named site1.com and site2.com. I run the a2ensite command to create the links to sites-enabled.

All redirections are working perfectly, except for site1.com using Chrome.

On Chrome, if I type www.site1.com it redirects me to the right folder /var/www/site1.com , if I type http://site1.com it redirects me to the wrong folder /var/www

Now this is where it gets bizarre, when I type www.site2.com it redirects me to /var/www/site2.com and when I type http://site2.com it redirects me correctly to /var/www/sites2.com

What I don't get, is the virtual host files are identical bar the actual ServerName & Alias and log locations.

Site1

<VirtualHost *:80>
        ServerAdmin webmaster@site1.com
        ServerName site1.com
        ServerAlias www.site1.com
        DocumentRoot /var/www/site1.com

        #<Directory />
        #        Options FollowSymLinks
        #        AllowOverride None
        #</Directory>
        #<Directory /var/www/site1.com>
        #        Options Indexes FollowSymLinks MultiViews
        #        AllowOverride None
        #        Order allow,deny
        #        allow from all
        #</Directory>


        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        ErrorLog /var/www-logs/site1.com/error.log
        CustomLog /var/www-logs/site1.com/access.log combined
</VirtualHost>

Site2

<VirtualHost *:80>
        ServerAdmin webmaster@site2.com
        ServerName site2.com
        ServerAlias www.site2.com
        DocumentRoot /var/www/site2.com

        #<Directory />
        #        Options FollowSymLinks
        #        AllowOverride None
        #</Directory>
        #<Directory /var/www/site2.com>
        #        Options Indexes FollowSymLinks MultiViews
        #        AllowOverride None
        #        Order allow,deny
        #        allow from all
        #</Directory>


        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        ErrorLog /var/www-logs/site2.com/error.log
        CustomLog /var/www-logs/site2.com/access.log combined
</VirtualHost>

Best Answer

I had this same issue and it was driving me nuts. I searched everywhere for any issues with Apache as I thought it was doing the redirecting.

Apparently this was a chrome (possibly Mac OS) issue. Firefox shows the Virtual Hosts as expected.

You can try flushing the DNS cache in the browser. (this is what worked for me temporarily though)

Visit: chrome://net-internals/#dns in a new chrome tab.

Then click on the clear host cache button.

Try revisiting the urls again and see if the right sites show up.

But that didn't work...

So that was not a permanent solution as it only worked the first time I did it and I started having problems again.

This solution worked for me: https://stackoverflow.com/a/30489627/754854

Basically, just go to System Preferences > Network > Advanced

In the DNS tab add your localhost IP 127.0.0.1. Also, be careful that you don't override your routers IP. For some strange reason the + sign removes it and replaces it with your configurations instead of just adding yours. If thats the case you will want to add your routers IP back in. You can see your routers IP on the Network page. Without it you may have a hard time loading websites so make sure its still there.

The order matters. Make sure your localhost IP is first and your router, or whatever else was setup, is second:

127.0.0.1
xx.xx.xx.x

Apply your changes and test it out.

This issue was happening for me on Mac OS Sierra using Chrome version 60.0.3112.101 (Official Build) (64-bit)

Related Topic