haybeye
haybeye

Reputation: 131

.htaccess rewrite root domain to subdomain

i am trying to forward all traffic to my subdomain via htaccess.

i am using that .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule (.*) http://sub.domain.com/$1 [R=301,L]

its working but when i directly write my root domain to the address bar like: http://domain.com it stays on that url. i also want root domain to force forward to sub.domain.com

how to do that? help me out please.

Upvotes: 0

Views: 1674

Answers (1)

anubhava
anubhava

Reputation: 784898

You need to make starting www. optional in %{HOST_NAME}.

Use this rule:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]

Upvotes: 4

Related Questions