configure仅允许特定域使用.htaccess访问某些文件夹

时间:2013-05-31 14:31:23

标签: apache .htaccess

我的情况如下:

例如,我有一个网站http://www.example.com,我设置了一些子域名,例如http://video.example.comhttp://image1.example.comhttp://image2.example.com。在Apache虚拟主机设置中,它们使用相同的文件夹(例如/home/example/)。 (这两个域使用mod_cband进行不同的带宽设置)。

我有一个子文件夹/home/example/files/videos,我想只能从子域http://video.example.com/files/videos/访问它,但不能来自http://www.example.com/files/videos/或任何其他子域。

如何使用.htaccess文件配置?

3 个答案:

答案 0 :(得分:14)

将此代码放入/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]

答案 1 :(得分:2)

您可以检查主机,然后使用mod_rewrite.htaccess文件中创建301重定向来处理此主机;虽然如果您有访问权限,那么如果您在httpd.conf或包含的配置文件中执行此操作会更好。

最佳方案

由于看起来mod_cband想要为每个域使用不同的virtualhost,您可以将httpd.conf文件设置为这样,并在配置中包含重写规则。一些Web主机将执行此方法,其中主帐户站点为DocumentRoot,其他站点都嵌套在其目录下:

<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>

亚军

如果您使用的是托管服务提供商,则您无权访问httpd.conf个文件,并且他们未将域设置为主域的alias(每个域有一个单独的文件夹),然后你会在.htaccess的根目录www.mywebsite.com中编写你的规则,如下所示:

  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]

最开销

如果他们使用别名(其中所有内容都具有完全相同的文档根目录),那么您需要使用通常由所有人制定的.htaccess文件来检查所请求的主机名:

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]

答案 2 :(得分:-2)

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