When I enter in my domain like this
www.domain.com
everything works fine. But when I enter it like this
domain.com
I get the 'It works!' page. I'm a complete Apache newb so I'm not exactly sure whats going wrong here.
Traditional solution: in the httpd configuration, find:
ServerName www.domain.com
and add:
ServerAlias domain.com
However, this will give you a server that responds with the same content on domain.com
and www.domain.com
. It's generally considered preferable (particularly SEO-wise) to have only one canonical hostname. To do that, add a new virtual server for the redirect, instead of the alias.
<VirtualHost *:80>
ServerName domain.com
Redirect permanent / http://www.domain.com/
</VirtualHost>
This has to do with the Apache config as others have stated.
Simply added a ServerAlias to the apache config for your virtualhost will do it for you. I don't think that it is good practice to create another virtualhost have it redirect for you.
It is good SEO practice to direct everything to one or the other, I suggest using this in either you config file or in your .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Another possible solution is that the default site is catching the traffic. This worked for me - if you don't care about disabling the default site altogether:
a2dissite 000-default
service apache2 reload
I had this problem too, and found my solution here.
Basically, after an upgrade, Apache put a symlink in /etc/apache2/sites-available
. Deleting everything between and including <VirtualHost *:80>
and <VirtualHost>
got everything working for me again.
I have faced the exactly same problem. However, my solution was not among the ones already discussed, so it is worth to share.
In my scenario, I had upgraded Apache, but the symlink was not there, as in the case of @Hannele. I have few sites into the same VPS. All, except one, were working with or without www
using similar simple .conf as following:
<VirtualHost *:80>
ServerName mysite1.com
ServerAlias www.mysite1.com
DocumentRoot /home/myuser/apps/myappname1/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/home/myuser/apps/myappname1/public">
Options FollowSymLinks
AllowOverride all
Options -MultiViews
Require all granted
</Directory>
</VirtualHost>
By chance I have looked into the apachectl -V
, and there was a warning. As nothing had changed, but the upgrade of Apache and the reboot on my VPS, I'd thought it would worth to solve this warning and see ... and it worked! Bellow is the warning:
Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
I solved the warning as explained here, i.e.:
servername.conf
file: sudo vi /etc/apache2/conf-available/servername.conf
ServerName localhost
to servername.conf
sudo a2enconf servername
sudo systemctl reload apache2
And the sole site which did not worked without www
(redirected to "It Works" page) is now working!
My solution was different from all of those posted here so far.
Check your server's hostname. In my case, I had set the hostname of my server to be the same as one of my domain names e.g. example.com
. When someone tried to access http://example.com
, it resulted in Apache's default/blank page because it was serving the _default_
VirtualHost.
I presume this was happening beacuse Apache was resolving the default localhost address as example.com
. To fix it, I did two things (although either/or may work):
somethingelse
using hostname somethingelse
and editing /etc/sysconfig/network
.ServerName random_nonexistent_string
to the default VirtualHost block./etc/hostname
if the ServerName
is not specified. From ServerName Directive: "If no ServerName is specified, the server attempts to deduce the client visible hostname by first asking the operating system for the system hostname, and if that fails, performing a reverse lookup on an IP address present on the system."
Apr 24, 2017 at 6:44