juancrrn
juancrrn

Reputation: 375

Redirect non-www to www excluding subdomain

First of all, please don't mark this question as duplicate because I've tried all other answers and no one works for me. I have a subdomain called account.domain.com and this is the code in my .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

I want to redirect visits from domain.com to www.domain.com, this works fine. The problem comes when I access the subdomain account.subdomain.com and it redirects to www.account.subdomain.com/account/.php... I don't know why.

I've tested a lot of different codes but I get the same result. My site is hosted in a free account in Hostinger.es.

Upvotes: 1

Views: 567

Answers (1)

anubhava
anubhava

Reputation: 785246

If you want to target only main domain then use:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Upvotes: 1

Related Questions