【问题标题】:configure only allow specific domains to access certain folders using .htaccess配置只允许特定域使用 .htaccess 访问某些文件夹
【发布时间】:2013-05-31 14:31:23
【问题描述】:

我的情况如下:

例如,我有一个网站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 文件进行配置?

【问题讨论】:

  • 检查引荐来源网址是最常见的“解决方案”——用引号括起来,因为引荐来源网址不可靠。

标签: apache .htaccess


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    最佳方案

    由于看起来mod_cband 想要为每个域使用不同的virtualhost,因此您可以像这样设置httpd.conf 文件并在配置本身中包含重写规则。一些网络主机会这样做,其中主帐户站点是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]
    

    【讨论】:

      【解决方案3】:

      关于上面的问题和 cmets,我正在成功使用下面的代码,但希望允许从特定域下名为 paypal 的任何文件夹访问 - 访问同一域下名为 download 的任何文件夹。是否可以将此代码更改为驻留在“sales”文件夹中 - 并允许从其中名为 paypal 的任何文件夹访问其中名为 download 的任何文件夹 - 无论它们驻留在 sales 文件夹中的什么位置? IE。 - 允许从这些 url 中的任何一个访问 -

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

      还有更多

      即。 - 到这些网址中的任何一个 - 同时阻止任何不是来自 mydomain.com\sales

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

      还有更多

      -目前我将 htaccess 放置在每个下载文件夹中,以阻止除特定 paypal 文件夹 url 之外的任何地方的访问

      RewriteEngine On
      RewriteCond %{HTTP_HOST} !mydomain.com\sales\myplans\paypal\$
      RewriteRule (.*) - [F]
      
      • 在每个下载文件夹中成功使用,但希望任何名为“download”的文件夹仅允许从特定域销售文件夹下名为“paypal”的任何文件夹访问 - “mydomain.com/sales”

      放置在销售文件夹中的东西,允许其中任何名为 download 的文件夹可以从其中任何名为 paypal 的文件夹访问

      所以有可能吗,我是 htaccess 的新手,但如果可以做到这一点,它将消除数百个 htaccess 文件,将它们减少到一个 htaccess 文件!抱歉,所有文字只是想非常清楚我想要完成的事情......谢谢

      【讨论】:

        【解决方案4】:
        rewritecond %{HTTP_HOST} video.mywebsite.com [s=1]
        rewriterule files/videos/.* - [F]
        

        【讨论】:

          猜你喜欢
          • 2012-05-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-23
          • 1970-01-01
          • 2017-06-08
          • 1970-01-01
          相关资源
          最近更新 更多