How-to: Make a cannonical redirect with Apache

Me and my goldfish brain tend to always forget how to make a proper cannonical redirect with Apache. So this is just a post for myself so that I don’t need to Google for it every time and read through posts with grade-A bullshit in them :)

First, start off by enabling the rewrite module on the server if you haven’t already done so.

sudo a2enmod rewrite

Edit your sites config file

sudo nano /etc/apache2/sites-available/www.my-awesome-site.com

And add this inside the -element

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.my-awesome-site\.com [NC]
RewriteRule ^/(.*) http://www.my-awesome-site.com [R=301,L]

Restart the server and you now have cannonical redirect from non-www to www.