How to separate Sub Domain from Main Domain using .htaccess?

If you are using shared hosting with active sub domains than you must have witnessed this problem of sub directory getting accessed via main domain URL. Now this is one of the biggest problem in shared hosting or i will say in any kind of hosting which uses cpanel.If you are unaware of this problem than let me explain you. Let us take a domain example.com as the main domain and we want to create domain demo.example .com as the sub domain. Creating this sub domain via cPanel results in the formation of demo.example.com directory your public_html  directory. Now you can access your sub domain via the following URL’s:

  • http://example.com/demo.example.com
  • http://demo.example.com

Here come’s the main problem. This will result in the problem of duplicate content and your site can face Google’s panda update hammer. In order to avoid this we will present before you the solution in the next para.

The solution to this problem lie in editing the .htaccess file located in the main domain folder. Add these lines of code in your.htaccess. I am assuming the domain to be example.com and sub domain to be demo.example.com.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/demo.example.com/(.*)$
RewriteRule ^(.*)$ – [L,R=404]