1

I'm trying to achieve a redirect from example.com to www.example.com (non-www to www).

My sites-available/example.com.conf file looks like this;

<VirtualHost *:80 *:443>
        ServerName www.example.com
        ServerAdmin unidan@example.com

        DocumentRoot /var/www/example.com/public_html

        ErrorLog /var/www/example.com/logs/error.log
        CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>

<VirtualHost *:80>
        ServerName example.com
        Redirect permanent / http://www.example.com
</VirtualHost>
<VirtualHost *:443>
        ServerName example.com
        Redirect permanent / https://www.example.com
</VirtualHost>

My sites-available/000-default.conf file looks like this;

<VirtualHost *:80>
        ServerAdmin zuckerberg@facebook.com
        DocumentRoot /var/www/default/public_html

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

Now.. when requesting http://www.example.com it will respond as requested. However, when requesting http://example.com it will render the request using the configuration in 000-default.conf.

Unlinking 000-default.conf from sites-enabled/ will fix this issue, however this is not something I want to do (I want to keep a default behaviour for unknown requests).

What is the issue here? Why does the configuration in 000-default.conf override the redirect?

1 Answer 1

2

Your domain.com virtual host only specifies www.example.com as a name, therefore Apache will only match it when www is specified.

ServerName www.example.com

You probably want to use the following configuration instead:

ServerName example.com
ServerAlias www.example.com

Edit: I am guessing, in your case, that Apache tries to match your virtual hosts in an order similar to the following:

www.example.com > default > example.com (80) > example.com (443)

Since the default host matches all requests sent on port 80, the two others would never be tested. This also explains why removing the default makes your configuration work correctly.

I am not very familiar with Apache's virtual host matching, but you may find more information about it on this page.

4
  • What about the 2 virtual host configs beneath it?
    – William
    Sep 3, 2014 at 22:05
  • It all depends on the order in which Apache tries to match the VirtualHosts. It seems like, in your case, the default host is tested right after www.domain.com. By the way, using ServerAlias for the www subdomain is quite a common practice. I added some more information in my answer. Sep 3, 2014 at 22:07
  • Yeah but I want to issue the Redirect in the Apache layer. How do I achieve this?
    – William
    Sep 3, 2014 at 22:10
  • Why would you want a redirect? Using both ServerName and ServerAlias will have Apache handle your requests through the correct virtual host, therefore there would be no need for redirection. If you want to force the use of www, I'd suggest you have a look here instead. This link uses .htaccess files but you may use your Apache configuration just as well. Sep 3, 2014 at 22:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.