1

It is actually a common questions and you find several posts for this (i.e. .htaccess - how to force "www." in a generic way?). But all redirects I tried don't work for me. www. will be added but all parameter will be lost.

mystore.com/store/view/ --> Redirects to ---> www.mystore.com/index.php

mystore.com/product/view/10 --> Redirects to ---> www.mystore.com/index.php

This are some I tried and which supposed to work:

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

OR

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

Could anyone please point me in the right direction? Thank you so much.

Community
  • 1
  • 1
Stefan
  • 6,536
  • 3
  • 25
  • 37

1 Answers1

1

You can use this code in your root .htaccess:

RewriteEngine On

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

RewriteRule ^index\.php$ - [L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L,NC]
anubhava
  • 664,788
  • 59
  • 469
  • 547
  • Your first rule will remove anything that comes from the non-WWW URL rather than keep it and redirect internally to the `index.php`, you should have just kept his first example rule which should work just fine and yet keep the HTTP/S translation in case he is using HTTPS at all. – Prix Jul 16 '14 at 19:49
  • ok I added it now but I got confused by 2 example OP provided that asked to be redirected to `www.mystore.com/index.php` – anubhava Jul 16 '14 at 20:00
  • After reading his question, this is what I understood, he already got the non-WWW to WWW working however it doesn't lead it to his controller(index.php) and he wanted an additional rule to do that. On both of his redirect examples he wants to send the coming URL to the controller, which your later rule does just fine ;) hence the +1. – Prix Jul 16 '14 at 20:04
  • 1
    Thanks @Prix, understanding right intent behind `mod_rewrite` questions is tricky sometimes. – anubhava Jul 16 '14 at 20:08
  • Very true, my friend. – Prix Jul 16 '14 at 20:10
  • 1
    Thanks everyone for your help. I really appreciate it! – Stefan Jul 16 '14 at 22:15