Learn, how to configure your Apache HTTP web server virtual host or .htaccess file to redirect from HTTP to HTTPS and domain (example.com) to subdomain (www.example.com).

Apache HTTP web server and SSL secured websites.

Apache HTTP is one of the most popular web servers on the internet and your website is probably served by one. If you have installed an SSL certificate for your website, then your web server probably serves 2 or even 4 parallel versions of your website. The first one is served via HTTP on port 80 and the second one is served via HTTPS on port 443.

1  http://www.micski.dk    80
2  http://micski.dk        80
3  https://micski.dk      443
4  https://www.micski.dk  443

This is not optimal, and could disturb your relation with search engines and ranking. You should configure your web server, so it redirects all variations to one single version of your website. This means, that it should redirect HTTP requests to HTTPS, and, that it should redirect domain to subdomain or subdomain to domain, if you prefer that.

Examples of correct HTTP to HTTPS and domain to subdomain redirection.

If your HTTP to HTTPS redirection is configured correct, then the following examples of redirections will happen. Note, how not only HTTP is redirected to HTTPS, but also domain to subdomain is redirected. Also note, that each redirection is to the correct website. There are no intermediate hops between the variations.

http://www.micski.dk   -->  https://www.micski.dk/
http://micski.dk       -->  https://www.micski.dk/
http://micski.dk/blog  -->  https://www.micski.dk/blog/
https:/www.micski.dk   -->  https://www.micski.dk/

Examples of correct Apache HTTP web server response codes.

If your web server does the redirection correct, it will first produce the response code 301 Moved Permanently and then the response code 200 OK. See the following example, where the responses are tested from the command line on a FreeBSD system, but if you do not have access to such a system, then you can also use a free online web based tool for testing web sites and servers. The free HTTP Status Tool by WebFX is a simple and neat tool for testing HTTP to HTTPS responses.

$ curl -I http://www.micski.dk
HTTP/1.1 301 Moved Permanently
Location: https://www.micski.dk/
$ curl -I https://www.micski.dk
HTTP/1.1 200 OK

Note, that Apache HTTP responses are also known as HTTP response codes, HTTP status codes, HTTP statuses and HTTP error codes.

Also note, that some websites, that has firewall and security features, such as Wordfence for WordPress, can mistake test requests as problematic and produce an alternative HTTP response code, such as 406 Not Acceptable. An example of this is, if you only request the headers, but not the content.

Redirect HTTP to HTTPS in Apache virtual host configuration.

This is the preferred method. Find the section for your website and configure the redirection as seen in the following example. Note, that you should make sure, that you do not also have conflicting redirection in your .htaccess file for your website. Also note the slash after each redirection target. Without it, then subfolders will not work.

<VirtualHost *:80>
  ServerName www.micski.dk
  ServerAlias micski.dk
  Redirect permanent / https://www.micski.dk/
<VirtualHost *:443>
  ServerName www.micski.dk
  ServerAlias micski.dk
  <If "%{HTTP_HOST} == 'micski.dk'">
    Redirect permanent / https://www.micski.dk/

Redirect HTTP to HTTPS in .htaccess configuration.

If you do not have access to Apache virtual host configuration, then you can configure the redirection in .htaccess.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^micski\.dk [NC]
  RewriteRule ^(.*)$ https://www.micski.dk/$1 [R=301,L]

More about the Apache web server.

Redirecting and Remapping with mod_rewrite.
Apache Module mod_rewrite.
Apache HTTP Server Project.
⧐ See a list of common HTTP response codes on HTTP Status Codes.