– Check HTTP/HTTPS & www/non-www redirects

How to configure canonical redirects in Apache?

It's pretty simple. This short how-to assumes the non-www domain name is the canonical one. Replace example.com with your domain.

In the <VirtualHost> configuration for HTTP, add the following directives.

<VirtualHost *:80>
    # This VirtualHost handles requests for both
    # http://example.com and http://www.example.com
    ServerName example.com
    ServerAlias www.example.com

    ...

    # Apache expands the URL's path automatically so that a request
    # to http://www.example.com/blog is redirected to https://example.com/blog.
    # `permanent` tells Apache to use the (recommended) 301 status code.
    Redirect permanent "/" "https://example.com/"
</VirtualHost>

In the <VirtualHost> configuration for HTTPS, add the following:

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

    <If "%{HTTP_HOST} =~ /^www\./">
        Redirect permanent "/" "https://example.com/"
    </If>

    ...
</VirtualHost>

Restart Apache and check your redirects on this website.