2

On a shared hosting I have a public_html directory where the site is located. There are multiple subdomains created and they are located in the public_html like public_html/subdomain1, public_html/subdomain2 etc.

This makes these sub sites accessible in two ways:

1.) domain.com/subdomain1

2.) subdomain.domain.com

What I need is to forbid the first option for every subdomain.

5
  • have you tried htaccess Jan 20, 2017 at 13:41
  • Thank you @JacobEvans, but that would deny the access to the subdomain in both cases. However, I need the case no 2, to be accessible.
    – Domas
    Jan 20, 2017 at 14:59
  • no, only put the htaccess file in public html to reference the subfolder Jan 20, 2017 at 15:06
  • Best solution is to arrange all domains so that their root directories are in separate places. For example, public_html/domain1, public_html/domain2 etc? Jan 20, 2017 at 15:08

1 Answer 1

1

This was the first question I stumbled upon while looking for this, and unfortunately it didn't get answered in over 5 years. So I figured it out and would like to share my approach here.

Using an <If> statemente (available from Apache 2.4 onwards), here's what you can do:

Put the following in an .htaccess file in the subdirectory.

<If "%{HTTP_HOST} != 'mysubdomain.mydomain.tld'">
    Deny from all
</If>

If you need more control, e.g. to allow access for multiple (sub)domains, you can also you use RegEx:

<If "! %{HTTP_HOST} =~ m#^mysubdomain\.mydomain\.tld$#">
    Deny from all
</If>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .