I have this virtualhost in /etc/apache2/sites-available/cv.conf

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/cv/web
    <Directory /var/www/cv/web>
        AllowOverride All
        Order Allow,Deny
        Allow from All

    ErrorLog ${APACHE_LOG_DIR}/cv_error.log
    CustomLog ${APACHE_LOG_DIR}/cv_access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

The lines Rewrite* have been added by letsencrypt and I checked on google, it seems to be the good way to do it. But when I go to domain.com it doesn't redirect me to https://example.com (it loads the 000-default.conf). Nevertheless it is redirecting me to https://www.example.com when I go on www.example.com

Do you have any idea? I don't know how to debug this. The logs are confirming what I am saying, the cv_access.log does not print any request when I go on domain.com.

1 Answer 1


Let's take a look at this line:

RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

Here, the %{SERVER_NAME} variable depends on the UseCanonicalName, by default set Off. Therefore, the variable has the content of Host: header, not the one specified in ServerName.

With UseCanonicalName On Apache httpd will use the hostname and port specified in the ServerName directive to construct the canonical name for the server.

With UseCanonicalName Off Apache httpd will form self-referential URLs using the hostname and port supplied by the client if any are supplied (otherwise it will use the canonical name, as defined above).

If you prefer to keep using mod_rewrite for this redirection, you have two options:

  1. Set UseCanonicalName On.
  2. Use the desired hostname directly in your rule, e.g.

    RewriteRule ^ https://example.com%{REQUEST_URI} [END,NE,R=permanent]

However, you are over-complicating things, as you could do the same without mod_rewrite:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com

    Redirect / https://example.com/

Here, the the Redirect Directive comes from mod_alias, and you don't need DocumentRoot etc.

  • I am afraid that the UseCanonicalName On doesn't work. Or do I have to hardcode the hostname in my rule plus the canonical name?
    – onda47
    Jul 19, 2017 at 9:33
  • The last example is way superior to any mod_rewrite solutions. Use it. You only need those five lines! Jul 19, 2017 at 9:44
  • I think the problem comes from somewhere else, it doesn't work either. Sorry :/
    – onda47
    Jul 19, 2017 at 9:52
  • Have you actually enabled the site as it's in sites-available? The a2ensite helps with that. Maybe the www is configured somewhere else, thus working. Jul 19, 2017 at 10:41
  • I have checked by executing grep 'www\.' /etc/apache2/sites-available/* but there is only results for cv.conf and cv-le-ssl.conf and I have checked that the Virtualhost is enabled :/
    – onda47
    Jul 19, 2017 at 11:19

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.