htaccess domain.tld to www.domain.tld redirect

I like to redirect my users from to Of course there are many solutions for this on the application side. But if you're using apache to host your website, there is also a very easy solution using mod_rewrite. Just add the following into your existing (or create a new) .htacess file:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

With this you're getting a 301 redirect for every single URL without any kind of redirect in your application.