Forum Moderators: phranque
How do I via the .htaccess file disallow visitors to enter via a directory URI, which also works as a subdomain?
In other words, I want to allow this:
[sub.domain.com...]
And by default give some sort of error page when vistors try to enter like this:
[domain.com...]
Well, and how do I keep searchbots from indexing websites with the IP address (as some of then do)?
TIA
Edit: I have no clue as to how I ended up in "Domain Names". I pushed the "Post New Topic" button in the "Apache Web Server" forum.
For starters, a RewriteCond by itself will do nothing. From the docs:
The RewriteCond directive defines a rule condition. Precede a RewriteRule directive with one or more RewriteCond directives. The following rewriting rule is only used if its pattern matches the current state of the URI and if these additional conditions apply too.
So, to correct the previous example:
RewriteEngine on
RewriteCond %{THE_REQUEST} /sub/ \ HTTP/
RewriteRule ^.* - [F]
...however, this is insufficient; what if 'sub.domain.com' has a directory called '/sub/' (unlikely, but *possible*)?
So, we check the Host header:
RewriteEngine on
#
# check that the host header as at least one character;
# that way we know it's been sent by the browser
RewriteCond %{HTTP_HOST} .
#
# Check the host header; if it's not 'www.domain.com',
# (or 'domain.com'), we don't care about it for this
# Rule
RewriteCond %{HTTP_HOST} ^(www.)?domain.com
#
# Ok, we can Rewrite this
RewriteRule ^/sub/ - [F,L]
If you have Lots(tm) of subdomains, there are a couple of ways to handle it. For, say, 5 or 6 of them, you could alter the RewriteRule above like this:
RewriteRule ^/(sub1¦sub2¦sub3¦sub4)/ - [F,L]