Reputation: 13534
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
Upvotes: 5
Views: 4079
Reputation: 3998
Check this solution. Short and Simple.
http://www.codefleet.net/prevent-access-to-subdomain-folder-from-main-domain/
Upvotes: 1
Reputation: 13534
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/
Upvotes: 5
Reputation: 143846
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.
Upvotes: 2