5

I have web site hosted on shared hosting account which is managed by CPanel. It allows to create unlimited sub-domains. I have the following situation:

Suppose that my primary domain there is: mydomain.com My account's files are found on the hosting server at /home/myaccount/public_html I decided to make sub-domain named dir i.e dir.mydomain.com I created this sub-domain to use the following file: /home/myaccount/public_html/Hosts/directory

Now I, successfully, able to access http://dir.mydomain.com However, I need to prevent the access http://mydomain.com/Hosts/directory and exclusively restrict the access to the sub-domain.

How could I achieve this using .htaccess?

The following is a copy of the code I use in the .htaccess file found in /home/myaccount/public_html/Hosts/directory

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
 </IfModule>
AddType audio/mpeg mp3
AddType text/xml xml   
SaidbakR
  • 13,303
  • 20
  • 101
  • 195

3 Answers3

5

The above answer of Jon Lin does not work. I found that Redirect may solve the problem as follows:

#.htaccess at public_html

Redirect /Hosts/directory/ http://dir.mydomain.com/
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
2

Leave the rules that are in /home/myaccount/public_html/Hosts/directory where they are, you need to add rules to your main document root, /home/myaccount/public_html of your primary domain so that it can't access stuff in Hosts:

RewriteRule ^/?Hosts - [L,R=404]

You'll need to add this before any rules you may already have that does routing. From your main domain, any accesses to /Hosts or any subdirectory will result in a 404.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
1

Check this solution. Short and Simple.

http://www.codefleet.net/prevent-access-to-subdomain-folder-from-main-domain/

Tharindu Thisarasinghe
  • 3,846
  • 8
  • 39
  • 70