Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from stackoverflow.com Jul 15 '11 at 5:23

This question came from our site for professional and enthusiast programmers.

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.

share|improve this answer
    
+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/… – therobyouknow Jan 9 '12 at 17:07
    
The OP wants to block me.com/mom, however, your first three rulesets block mom.com/mom. ".htaccess files are scanned from the top level and down" - actually, it's the other way round... from the bottom level and up. But mod_rewrite directives are not inherited anyway by default so this shouldn't be a problem. ". which means 'catch all'" - The dot matches a single char, it's not a "catch-all", it will fail to match the bare domain (empty URL-path). To make it a catch-all you need .* (0 or more) or simply ^. – w3dk Dec 18 '15 at 19:32

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.

share|improve this answer

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.

share|improve this answer
    
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. – w3dk Dec 18 '15 at 19:38

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|improve this answer
    
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). – w3dk Dec 18 '15 at 19:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.