Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

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 hosting account where I have my main site (www.mainsite.com points to root folder), and a subfolder xpto.com, where I want to have another site (www.xpto.com).

I can access www.anothersite.com by typing www.mainsite.com/xpto.com. Also, using cPanel, I've redirected www.xpto.com to www.mainsite.com/xpto.com.

My question is: is there a way to prevent www.xpto.com access by typing www.mainsite.com/xpto.com, so it can be accessed only by typing www.xpto.com?

EDIT: also, I don't want to www.xpto.com to be accessed via www.xpto.mainsite.com. Any help?

Hope I have explained this clearly :)

share|improve this question

migrated from stackoverflow.com Nov 2 '11 at 12:07

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

up vote 3 down vote accepted

If mod_rewrite is enabled on your server you may create following .htaccess file in the folder root_folder/xpto.com/:

RewriteEngine on
RewriteCond %{HTTP_HOST} mainsite\.com$ [NC]
RewriteRule .* - [F]

This will check if the request URL ends with mainsite.com and deny access if it does.

EDIT: If you want to return a 404 Not Found, you may use this RewriteRule (first two lines stay the same):

RewriteRule .* - [R=404]

You can also insert other HTTP status codes instead of 404 (but replace the dash with an actual URL for redirects).

share|improve this answer
Thank you Beat. I tried, and it worked, I mean, I get a Forbiden error when accessing xpto.mainsite.com or mainsite.com/xpto.com. However, is it possible to customize this error, for instance, change it to "Not Found" error? Thanks! – escar Nov 1 '11 at 18:21
Thanks Beat! Worked like a charm ;) – escar Nov 1 '11 at 19:36

Your Answer


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