How to create .htaccess to block access to a subdirectory that hosts a separate domain

5,313

Solution 1

Edit: Fixed an error!

This is the theory:

RewriteEngine on
RewriteCond %{HTTP_HOST} mom.com$ [NC]
RewriteRule ^mom/ - [R=404,L]

On some versions, if the above doesn't work, you can do this ugly trick

RewriteEngine on
RewriteCond %{HTTP_HOST} mom.com$ [NC]
RewriteRule ^mom/ /someurlthatdoesntexist [L]

If you're fine with a 403 forbidden instead of a 404 not fund, you can use the F option.

RewriteEngine on
RewriteCond %{HTTP_HOST} mom.com$ [NC]
RewriteRule ^mom/ - [F,L]

However, there's one more thing to consider. .htaccess files are scanned from the top level and down, so if mom.com has any .htaccess files, they will take precedence. What you can do then, is the add the following to the .htaccess files in mom.com. This should be at the top of the files (right after RewriteEngine On).

RewriteEngine on
RewriteCond %{HTTP_HOST} !mom.com$ [NC]
RewriteRule . - [R=404,L]

Note the added !, which means match if the domain is not mom.com. Also note the . which means "catch all".

Replace - [R=404,L] with any of the other patterns as needed.

Solution 2

Thanks for the input, I based on your help I got it working, but in a slightly different way: I found this article on BlueHost (my host) that had the correct code to get it working:

The code I used was:

Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?me.com$ [NC]
RewriteCond %{REQUEST_URI} ^/mom/(.*)$
RewriteRule ^(.*)$ - [L,R=404]

I think the REQUEST_URI was what was missing.

This works in either the public_html or the /mom/ subdirectory .htaccess files.

Solution 3

Both the solutions of Stan and nitro2k01 didn't work for me.

The following did work for me:

# Place this .htacces file in /mom subdirectory
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mom\.nl$ [NC]
RewriteRule ^(.*)$ - [L,R=404]

In the public.www folder, I used the following to get it working:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mom\.nl$ [NC]
RewriteRule .? mom/%{REQUEST_URI} [L,QSA] 

Hope this might help others.

Solution 4

You can use this rule in your .htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?me\.com [NC]
RewriteRule ^mom/.*$ /some-error-page.html [L,R]
Share:
5,313

Related videos on Youtube

MrWhite
Author by

MrWhite

Updated on September 17, 2022

Comments

  • MrWhite
    MrWhite over 1 year

    I have a main domain (me.com) and I am hosting files for my mom's website (mom.com) in a subdirectory in my public_html folder so that mom.com uses files from me.com/mom.

    All I want to do is make it so that anyone trying to access her site through me.com/mom gets an error, without disturbing the visitors to mom.com.

  • therobyouknow
    therobyouknow over 12 years
    +1 But does this present a 404 error if the subdirectory is accessed? An improvement on this idea might be to stop results appearing in, for example, search results in the first place, here's a suggestion: webmasters.stackexchange.com/questions/24360/…
  • MrWhite
    MrWhite over 8 years
    It would be more efficient to remove the REQUEST_URI condition and use the pattern in the RewriteRule instead. eg. RewriteRule ^mom/ - [R=404,L] - although this would need to go in the parent (me.com) .htaccess file.
  • MrWhite
    MrWhite over 8 years
    Note that if you are Redirecting, then you must use a root-relative (starting with a slash) or absolute URL in the substitution. Or set the RewriteBase. If you specify a relative substitution then you will get "strange" results when it redirects (as the directory-prefix will be added).