Search code examples
.htaccesshttp-redirectsubdomain

How to redirect non WWW to WWW with .htaccess


I know this is a duplicate question but I tried all the answers here on stackoverflow and many results on google but nothing works.I even tried to use this htaccess redirect generator http://www.htaccessredirect.com/ that gave me this code

## Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^a.solutionbanks.com[nc]
RewriteRule ^(.*)$ http:## www.a.solutionbanks.com/$1 [r=301,nc]

that also didn't work. all what I am trying to do is to redirect from a.solutionbanks.com to --> www.a.solutionbanks.com


Solution

  • Your syntax is wrong at many places. Try this rule:

    Options +FollowSymLinks
    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^a\.solutionbanks\.com$ [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
    

    Reference: Apache mod_rewrite Introduction

    Apache mod_rewrite Technical Details