When I enter in my domain like this


everything works fine. But when I enter it like this


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/
| improve this answer | |

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]
| improve this answer | |
  • Why do you not think it is good practice to have a virtualhost do the redirect? It is simpler and has less performance impact than processing everything with mod_rewrite. – bobince Dec 14 '09 at 3:49
  • I think it is partly to do with preference, but if you have to manage hundreds of virtualhosts, it can become cumbersome to keep all these 'stub' virtualhost up to date and organized. I think there use to be a problem with apache loading up a lot of virtualhost, not sure if this is a problem with apache still. If you are just hosting one or two sites it isn't too big of a deal. – Ryan Gibbons Dec 14 '09 at 14:34

Check the mod_alias at your apache config file, make sure that the regex is correct for redirection.

| improve this answer | |
  • I don't understand the previous -1, checking if mod_alias is enabled is a good tip. – Seb Apr 22 '16 at 18:55

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
| improve this answer | |

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.

| improve this answer | |

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

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 for ServerName

I solved the warning as explained here, i.e.:

  1. Create a servername.conf file: sudo vi /etc/apache2/conf-available/servername.conf
  2. Add ServerName localhost to servername.conf
  3. Run sudo a2enconf servername
  4. Reload apache: sudo systemctl reload apache2

And the sole site which did not worked without www (redirected to "It Works" page) is now working!

| improve this answer | |

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):

  1. Changed my hostname to somethingelse using hostname somethingelse and editing /etc/sysconfig/network.
  2. Added a random ServerName random_nonexistent_string to the default VirtualHost block.
| improve this answer | |
  • You should not change your hostname for this! Sometimes that could even cause other problems on another services. Apache only falls back to /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." – Esa Jokinen Apr 24 '17 at 6:44
  • @EsaJokinen That explains why it used my hostname, because I had not specified a ServerName for the default VirtualHost. Changing my hostname was not a risk because I don't use it for anything else. – WackGet Apr 24 '17 at 16:12

Your Answer

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