Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

So I am pretty noob at with creating code in .htaccess, but I know it is what I need to accomplish a pretty simple task. So here is what I want to do.

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).

Thanks in Advance

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.

4 Answers 4

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

Can you try this rule in your .htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^me\.com [NC]
RewriteRule ^/mom/.*$ some-error-page.html [L,R]
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:

http://www.bluehost.com/cgi/help/498

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

Thanks again for the help!

share|improve this answer

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

Your Answer

 
discard

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