Redirecting all requests to https and www using .htaccess in Apache

September 04, 2018 [Tech]

I want all requests to artificialworlds.net/rabbit-escape/levels/ to get redirected to use the https protocol, and to include "www." at the beginning of the URL, and I found lots of Stack Overflow articles, but nothing there worked perfectly for me. Here is how I managed it.

I edited the .htaccess file in the directory where I want this to apply, and added this at the top:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^artificialworlds\.net$ [NC]
  RewriteRule ^ https://www.artificialworlds.net%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTPS} off
  RewriteRule ^ https://www.artificialworlds.net%{REQUEST_URI} [R=301,L]
</IfModule>