2

Plot: I actually like www URL's but not when used with subdomain like www.whatever.example.com . First i was using two domains on my hosting account so I added a htaccess rewrite like below code.

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

Motive: Now after sometime I realised that I need a subdomain also pointed to my hosting account. But I don't want a ugly subdomain like www.whatever.example.com but at the sametime I want to redirect example.com users to www.example.com. I am unable to figure out how to do that. Anyone here.

anubhava
  • 761,203
  • 64
  • 569
  • 643
IdidntKnewIt
  • 572
  • 6
  • 23

2 Answers2

3

You can use this rule to only target your main domain:

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

Or else, to target any sub-URL down to 3 levels 'deep':

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^[^.]+\.[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
BE77Y
  • 737
  • 6
  • 8
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

Try this using .htaccess, Ref

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Community
  • 1
  • 1
Zeeshan
  • 1,659
  • 13
  • 17