Forum Moderators: phranque

Message Too Old, No Replies

Limit access to a folder's contents by subdomain

         

ocon

2:00 am on Oct 22, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



I'm using a cms program that utilizes a wildcard dns setting to serve different accounts on different subdomains. However, I don't create the different subdomains with different content, it's just one regular directory, with the different subdomains pointing to it, with the cms folder deciding what to output.

One thing to note is that if I create the folder with the file /folder/test.txt, then I can read it on:
www.example.com/folder/test.txt
1.example.com/folder/test.txt
2.example.com/folder/test.txt
3.example.com/folder/text.txt

I don't want this. I only want /folder/ to be able to be accessed through the www subdomain.

Is there an .htaccess line I can create and put in /folder/ to prevent access through other domains?

Thanks

jdMorgan

3:06 pm on Oct 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do you want to happen if it's accessed from a subdomain?

Jim

ocon

3:46 pm on Oct 22, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



I'd like it to apper as if it didn't exist.

jdMorgan

7:12 pm on Oct 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That would be a 404-Not Found then...

Are there links to it already? if so, one of the following would be better:
301 redirect back to subdomain root
301 redirect somewhere else
403-Forbidden response

Also, what version of Apache are you on?

Jim

ocon

1:25 pm on Oct 25, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thinking about the others, I still think I would want a 404 error to return.

My Apache version is 2.2.13.

jdMorgan

1:49 pm on Oct 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming you've got other working rewriterules - that is, that you have already set up and enabled mod_rewrite, and that this code goes into /.htaccess, then for Apache 2.x, you could use:

RewriteRule ^folder/ - [R=404,L]

If you want to put the code into /folder/.htaccess, then the RewriteRule pattern would be simply "^" instead of "^folder/".

Jim

ocon

6:16 pm on Oct 25, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thank you for your reply, but wouldn't this block all visits to this folder, even users from www.example.com/folder/test.txt?

Again, the subdomains have access to the same folder with the same contents, but I only want the www subdomain to be able to see it.

jdMorgan

7:04 pm on Oct 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you'll need two further qualifiers:

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com\.?(:[0-9]+)?$
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /folder/
RewriteRule ^folder/ - [R=404,L]

This code must go in your Web root .htaccess file.

Jim