How to redirect www or non-www using htaccess
Hi Flarumites!
After seeing a request for a guide on how to redirect from non-www to www on Discord, I thought I'd write up a short guide for those who are struggling. Please note that this is for Apache web servers and leverages htaccess. There are other methods of achieving this result, and I am not saying that one is necessarily better than the other.
Why should I redirect
Having a canonical address can be better for SEO rather than having your site on both www and non-www versions, particularly if one version is different to the other (broken images, links, etc).
In the below examples, you should replace yourdomain.com with your actual domain name.
Redirecting from www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
Redirecting from non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
The above codes should go at the very top of your htaccess file.