rewrite rule in htaccess

I have a website structured:

- root
  - .htaccess
  - wordpress installation
  - folder xxx
    - wordpress

In my .htaccess I have this rule:

Read More
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

The problem is that it don’t allow me to go in:

www.mywebsite.it/xxx 

because a loop redirection happen.
If I erase the rule, all goes good.
How can I modify it?
I want that if user type mywebsite.it/ or mywebsite.it/xxx, it is redirected to www version.

my .htaccess:

    <IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

Thanks a lot.

Related posts

Leave a Reply

1 comment

  1. This rule works in isolation but WordPress stuff is hijacking over it. Slightly modified rule:

    RewriteCond %{HTTP_HOST} !^www.
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    1. Make sure this is 1st rule after RewriteEngine On line in DOCUMENT_ROOT/.htaccess
    2. make sure in WP permalink structure you have site address with www. otherwise WP will again send it back to non-www
    3. If you have any .htaccess below root then they should this additional line below RewriteEngine On line:

      RewriteOptions Inherit
      

    Also test in a different browser to avoid 301 caching.