Forum Moderators: phranque

Message Too Old, No Replies

.htaccess - subdomain vs. directory

         

philaweb

10:25 pm on Apr 17, 2005 (gmt 0)

10+ Year Member



Okay, I'm totally blank as how to approach this one, have googled for hours to find someone else with a handy solution.

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.

jd01

2:56 am on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could use a condition:

RewriteCond %{THE_REQUEST} /sub/ \ HTTP/ -[F]

or something similar should work.

Justin

(I think my syntax is correct, but double check me on that.)

sitz

11:39 am on Apr 18, 2005 (gmt 0)

10+ Year Member



Actually, that won't work. Sorry. =)

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]

If you have tens (or hundreds. or thousands.) of subdomains, I'd look into using a RewriteMap; documentation on RewriteMaps is available at [httpd.apache.org ]

jd01

11:52 am on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sitz - Thanks.

That was my I'm falling asleep, can't think, but here's a direction message... I'll try to do better.

Justin