Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess enforce "www" yet allow subdomain access

I have looked for the solution everywhere but couldn't find the exact solution I am looking for. I am not good at htaccess stuff. Need some help.

I have enforced "www" in my htaccess file and its getting on the way when I tried to create a subdomain.

I am using this setting i found and so far its working fine without subdomain.

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

RewriteCond %{HTTP_Host} ^(www\.)?example\.net$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

After creating a subdomain, its addding "www" at front and getting redirected to main site instead of subdomain.

For example: folder.domain.com -> becomes -> www.folder.domain.com (displayes the main site)

What modification is needed on top of what I have in this htaccess?

  1. Enforce "www".
  2. Also allow subdomain if its not "www".
  3. plus subdomin URL from ".net" also redirects to ".com"

By the way my main site is in codeigniter and I also have this:

RewriteCond $1 !^(index\.php|js|media|style)
RewriteRule ^(.*)$ /index.php/$1 [L]

Wanted to install blog on subfolder and use it as subdomain.

easy way was to modify (add "blog")

RewriteCond $1 !^(index\.php|js|media|style|blog)
RewriteRule ^(.*)$ /index.php/$1 [L]

then I an access it as www.domain.com/blog

But I wanted it neat and clean way so that controller created URL will not conflict with sub-folders. Probably not allow access subfolders directly.

Thanks

like image 426
Laraveldeep Avatar asked Nov 01 '22 06:11

Laraveldeep


1 Answers

Have your rules like this:

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

RewriteCond %{HTTP_Host} ^(.+?)\.net$ [NC]
RewriteRule ^(.*)$ http://%1.com/$1 [L,R=301]
like image 96
anubhava Avatar answered Nov 14 '22 18:11

anubhava