I am looking for a working solution for the following problem:
I have many domains with same website.
When somebody goes to www.domainname.tld there should be redirection to http://www.domainname.tld
When somebody goes to subdomain.domainname.tld there should be redirection to http://www.domainname.tld
When somebody goes to domainname.tld there should be redirection to http://www.domainname.tld.
===========================
I tried the following:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{SERVER_NAME}/$1 [L,R=301]
===========================
Problem: When entering subdomain.domainname.tld there is redirection to www.subdomain.domainname.tld
===========================
I want it to be in .htaccess please.
I have searched already, but unfortunately without success.
I would be very happy if anybody could provide a solution :)
===========================
With your help I was able to change the code to:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%2\.%3/$1 [L,R=301]
I just would need a last change -> it should not direct to http://domain.tld -> I would prefer http://www.domain.tld
I have tried different attempts to add the www in the target - but without any success.
I ended with the Anonymous solution, which works well for me:
RewriteCond %{HTTP_HOST} !^www\.[^.]+\.[^.]+$
RewriteCond %{HTTP_HOST} ([^.]+\.[^.]+)$
RewriteRule (.*) http://www.%1/$1 [L,R=301]
Thanks to all who had time to read and help!