leon
leon

Reputation: 10395

configure only allow specific domains to access certain folders using .htaccess

I am having a scenario as following:

For example,I have a website http://www.example.com, and I have setup a few subdomains such as http://video.example.com, http://image1.example.com, http://image2.example.com. In Apache virtual host setting, they are using the same folder (e.g. /home/example/). (these two domains have different bandwidth setup using mod_cband).

I have a subfolders /home/example/files/videos, I want to make it only accessible from the subdomain http://video.example.com/files/videos/ but not from http://www.example.com/files/videos/ or any other subdomains.

How shall I configure this with a .htaccess file?

Upvotes: 10

Views: 19171

Answers (4)

Keith
Keith

Reputation: 1

in regards to the question and comments above I am using code below successfully, but would like to allow access from any folder named paypal under the specific domain - to any folder named download under the same domain. Can this code be altered to reside in the "sales" folder - and allow access from any folder inside it named paypal to any folder in it named download - regardless of where they reside inside the sales folder? ie. - allow access from any of these urls -

mydomain.com\sales\wherever\paypal\
mydomain.com\sales\whoever\anywhere\paypal\
mydomain.com\sales\whatumacallit\somewhere\else\paypal\

and many more

ie. - to any of these urls - while blocking anything that isn't from mydomain.com\sales

mydomain.com\sales\wherever\download\
mydomain.com\sales\whoever\anywhere\download\
mydomain.com\sales\whatumacallit\somewhere\else\download\

and many more

- currently I place place htaccess in each download folder to block access from everywhere except specific paypal folder url

RewriteEngine On
RewriteCond %{HTTP_HOST} !mydomain.com\sales\myplans\paypal\$
RewriteRule (.*) - [F]
  • Successfully using in each download folder but would like ANY folder named "download" to allow access from ANY folder named "paypal" under the specific domain sales folder only - "mydomain.com/sales"

something to place in sales folder that allows any folder inside it named download to have access from any folder inside it named paypal

So is it possible, I'm a rookie in htaccess but if this can be done it eliminates hundreds of htaccess files reducing them to one htaccess file! Sorry for all the text just trying to be very clear as to what I want to accomplish... Thanks

Upvotes: 0

anubhava
anubhava

Reputation: 785098

Put this code in /home/mywebsite/files/videos/.htaccess

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /files/videos/

# if it is not for domain video.mywebsite.com then block it
RewriteCond %{HTTP_HOST} !^video\.mywebsite\.com$ [NC]
RewriteRule ^ - [F]

Upvotes: 18

AbsoluteƵERØ
AbsoluteƵERØ

Reputation: 7870

You can check the host and then handle this with mod_rewrite creating a 301 redirect in an .htaccess file; although if you have access, it's much better if you do this in the httpd.conf or an included config file instead.

Best Scenario

Since it looks like mod_cband wants a different virtualhost for each domain, you would setup your httpd.conf file something like this and include the rewrite rules in the config itself. Some web hosts will do this method where the main account site is the DocumentRoot and the other sites are all nested under it's directory:

<VirtualHost *:80>
  ServerName www.mywebsite.com
  DocumentRoot /home/mywebsite/

  RewriteEngine on
  RewriteRule ^/files/videos/(.*)$ http://video.mywebsite.com/$1 [R=301,L]
  RewriteRule ^/files/images1/(.*)$ http://image1.mywebsite.com/$1 [R=301,L]
  RewriteRule ^/files/images2/(.*)$ http://image2.mywebsite.com/$1 [R=301,L]

</VirtualHost>

<VirtualHost *:80>
  ServerName video.mywebsite.com
  DocumentRoot /home/mywebsite/files/video/
</VirtualHost>

<VirtualHost *:80>
  ServerName image1.mywebsite.com
  DocumentRoot /home/mywebsite/files/images1/
</VirtualHost>

<VirtualHost *:80>
  ServerName image2.mywebsite.com
  DocumentRoot /home/mywebsite/files/images2/
</VirtualHost>

Runner up

If you're using a hosting service provider where, you don't have access to the httpd.conf files, and they haven't setup the domains as an alias of the main domain (each domain has a separate folder), then you would write your rules in the root .htaccess for www.mywebsite.com like this:

  RewriteEngine On
  RewriteRule ^(files/videos/.*)$ http://video.mywebsite.com/$1 [R=301,L]
  RewriteRule ^(files/images1/.*)$ http://image1.mywebsite.com/$1 [R=301,L]
  RewriteRule ^(files/images2/.*)$ http://image2.mywebsite.com/$1 [R=301,L]

Most overhead

If they are using aliases (where everything has the exact same document root) then you'll need to check the requested hostname with a .htaccess file that's commonly enacted by all:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^video.mywebsite.com$
RewriteRule ^(files/videos/.*)$ http://video.mywebsite.com/$1 [R=301,L]

  #Check to make sure if they're on the video domain
  #that they're in the video folder otherwise 301 to www
  RewriteCond %{HTTP_HOST} ^video.mywebsite.com$
  RewriteCond %{REQUEST_URI} !^/files/videos [NC]
  RewriteRule ^.*$ http://www.mywebsite.com/ [R=301,L]


RewriteCond %{HTTP_HOST} !^image1.mywebsite.com$
RewriteRule ^(files/images1/.*)$ http://image1.mywebsite.com/$1 [R=301,L]

  #Check to make sure if they're on the image1 domain
  #that they're in the images1 folder
  RewriteCond %{HTTP_HOST} ^image1.mywebsite.com$
  RewriteCond %{REQUEST_URI} !^/files/images1 [NC]
  RewriteRule ^.*$ http://www.mywebsite.com/ [R=301,L]

RewriteCond %{HTTP_HOST} !^image2.mywebsite.com$
RewriteRule ^(files/images2/.*)$ http://image2.mywebsite.com/$1 [R=301,L]

  #Check to make sure if they're on the image1 domain
  #that they're in the images2 folder
  RewriteCond %{HTTP_HOST} ^image2.mywebsite.com$
  RewriteCond %{REQUEST_URI} !^/files/images2 [NC]
  RewriteRule ^.*$ http://www.mywebsite.com/ [R=301,L]

Upvotes: 2

Ilya Novojilov
Ilya Novojilov

Reputation: 889

rewritecond %{HTTP_HOST} video.mywebsite.com [s=1]
rewriterule files/videos/.* - [F]

Upvotes: -2

Related Questions