How Redirect (301) Non-Www To A Www

Hi,

How can I redirect non-www url to www?

When I use www.my-domain.com it goes to my-domain.com.

But I want it to be always www.my-domain.com.

I have tried to add these three options (one by one), to my .htaccess but they dont work (website does not load):

RewriteCond %{HTTP_HOST} ^my-domain.com
RewriteRule (.*) http://www.my-domain.com/$1[R=301,L]

RewriteCond %{HTTP_HOST} ^my-domain.com [NC]
RewriteRule ^(.*)$ http://www.my-domain.com/$1[L,R=301]

RewriteCond %{http_host} ^my-domain.com [nc]
RewriteRule ^(.*)$ http://www.my-domain.com/$1[r=301,nc]

This is what I have in the .htaccess:

RewriteEngine on

RewriteCond %{REQUEST_URI} .*\/aw2\.html
RewriteRule ^.*$ http://my-domain.com[L,R=301]

# Please note that RewriteBase setting is obsolete use it only in case you experience some problems with SEO addon.
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart
RewriteBase /
Options -MultiViews

RewriteRule ^init.php$ - [F,L,NC]

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{REQUEST_URI} ^api/(.*)$ [or]
RewriteCond %{REQUEST_URI} .*/api/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*api/(.*)$ api.php?_d=$1 [L,QSA]

RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml)$ [or]
RewriteCond %{REQUEST_URI} store_closed.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/(.*)$ $2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]

Thank you very much for your help. =)

This snippet should works properly for HTTP and HTTPS:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|offs()
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

Hi,

Thank you for your quick response. =)

I have tried your snippet but it gives the same problem.

I think there is a redirect to a non-www already somewhere. So when I add the www redirect it makes a loop.

Firefox says: "Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

Ok.. The problem was in the Store settings. (Administration > Stores > Storefront URL).

Changed that and added the snippet from gleb.goncharov and it works great.

Thanks gleb.goncharov =)

Just change this part of your config.local.php

FROM:

// Host and directory where software is installed on no-secure server
$config['http_host'] = 'yourcompany.com';
$config['http_path'] = '';

// Host and directory where software is installed on secure server
$config[‘https_host’] = ‘yourcompany.com’;
$config[‘https_path’] = ‘’;




TO

// Host and directory where software is installed on no-secure server
$config['http_host'] = 'www.yourcompany.com';
$config['http_path'] = '';

// Host and directory where software is installed on secure server
$config[‘https_host’] = ‘www.yourcompany.com’;
$config[‘https_path’] = ‘’;

and your webmaster tools settings to your preferred domain with www