I have a main domain and a website setup on my cPanel hosting as well as an add-on domain with its own separate website: domain.com and addon.com
Due to the way cPanel configures add-on domains, the add-on domain's website can be accessed via the main domain:
addon.domain.com/
domain.com/addon/
What I'd like to do is block all access to the add-on domain's website from the main domain, including any and all files or folders contained within, so:
- Accessing addon.com/anyfolder/anyfile.php works, but;
- Accessing addon.domain.com/anyfolder/anyfile.php or
- Accessing domain.com/addon/anyfolder/anyfile.php fails with a 404 error.
I have had some success with modifying the add-on domain's htaccess file to achieve this, for example using this in the add-on domain's htaccess:
RewriteEngine on
RedirectMatch 404 ^/addon/(.*)$
Blocks domain.com/addon/anyfolder/anyfile.php perfectly, displaying a 404 error.
However I have tried various methods to do the same for addon.domain.com and although I can usually get it to display a 404 when accessing only the domain, for any folders or files (addon.domain.com/anyfolder/anyfile.php) it will still work with no error and I can't figure out how to go about correcting this with htaccess.
For example:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^https?://(www\.)addon\.com
RewriteRule ^(.*)$ - [L,R=404]
This makes addon.domain.com display the desired 404 error, but addon.domain.com/anyfolder/anyfile.php still works without issue.
This is just one example of course, I've actually searched for a while and tried several different rewrite conditions among other things to get the desired result without success. Another example that works only for the domain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?addon.domain.co.uk$ [NC]
RewriteRule ^(.*)$ - [L,R=404]
Once again, this causes addon.domain.com to display the desired 404 error, but addon.domain.com/anyfolder/anyfile.php still works.
The only other thing I can think of doing is editing the main domain's DNS zone and remove (or break somehow) the A record for the subdomain. This would of course result in a "server not found" error which would have the desired affect, but I can't imagine it's an advisable option to take.