限制对Web目录的访问,但允许子域

时间:2010-09-27 01:54:05

标签: php apache .htaccess

我试图拒绝直接访问的人访问子域目录,而不是让他们直接浏览子域。例如,我希望人们能够转到http://test.example.com而不是http://example.com/test。如果他们确实显示403 Forbidden错误。这可能通过htaccess吗?谷歌搜索它,但看不到任何人这样做。主要目的是禁止他们直接访问内容。试过以下没有成功。

<Directory /test>
  Order Deny,Allow
  Deny from all
  Allow from .example.com
</Directory>

1 个答案:

答案 0 :(得分:5)

您可以执行从example.com/test/test.example.com的永久重定向。这样可以确保所有人都在test.example.com下访问您的网站。这对您的用户更合适。

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example.com [nc]
RewriteRule ^test/(.*)$ http://test.example.com/$1 [r=301,nc] 

如果你真的想要展示403 Forbidden,你可以这样做

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example.com [nc]
RewriteRule ^test/(.*)$ / [r=403,nc] 
相关问题