8

我的情况如下:

例如,我有一个网站http://www.example.com,并且设置了几个子域,例如http://video.example.com, http://image1.example.com, http://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文件配置它?

4

3 回答 3

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]
于 2013-06-16T07:09:38.820 回答
2

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

最佳场景

由于看起来mod_cband需要为每个域使用不同的虚拟主机,因此您可以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为主域(每个域都有一个单独的文件夹),那么您将编写规则在这样的根.htaccesswww.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]
于 2013-06-13T05:52:19.840 回答
-2
rewritecond %{HTTP_HOST} video.mywebsite.com [s=1]
rewriterule files/videos/.* - [F]
于 2013-06-13T04:35:11.300 回答