2013-05-31 19 views
7

Tôi gặp một kịch bản như sau:cấu hình chỉ cho phép các lĩnh vực cụ thể để truy cập vào thư mục nào đó sử dụng .htaccess

Ví dụ, tôi có một trang web http://www.example.com, và tôi đã thiết lập một vài tên miền phụ như http://video.example.com, http://image1.example.com, http://image2.example.com . Trong cài đặt máy chủ ảo Apache, họ đang sử dụng cùng một thư mục (ví dụ: /home/example/). (hai miền này có thiết lập băng thông khác nhau bằng cách sử dụng mod_cband).

Tôi có một thư mục con /home/example/files/videos, tôi muốn làm cho nó chỉ thể truy cập từ các tên miền phụ http://video.example.com/files/videos/ nhưng không từ http://www.example.com/files/videos/ hoặc bất kỳ tên miền phụ khác.

Làm cách nào để định cấu hình tệp này bằng tệp .htaccess?

+0

Kiểm tra refe rrer là "giải pháp" phổ biến nhất cho điều này - trong dấu ngoặc kép, bởi vì liên kết giới thiệu không đáng tin cậy. – CBroe

Trả lời

12

Đặt mã này trong /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
rewritecond %{HTTP_HOST} video.mywebsite.com [s=1] 
rewriterule files/videos/.* - [F] 
2

Bạn có thể kiểm tra máy chủ và sau đó xử lý này với mod_rewrite tạo ra một chuyển hướng 301 trong một tập tin .htaccess; mặc dù nếu bạn có quyền truy cập, tốt hơn nếu bạn thực hiện điều này trong httpd.conf hoặc tệp cấu hình được bao gồm thay thế.

Kịch bản xuất sắc nhất

Kể từ khi nó trông giống như mod_cband muốn một virtualhost khác nhau cho từng lĩnh vực, bạn sẽ thiết lập một cái gì đó httpd.conf tập tin của bạn như thế này và bao gồm các quy tắc viết lại trong cấu hình riêng của mình. Một số máy chủ web sẽ làm phương pháp này, nơi các trang web tài khoản chính là DocumentRoot và các trang web khác đều được đặt nằm dưới đó là thư mục:

<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 lên

Nếu bạn đang sử dụng một nhà cung cấp dịch vụ lưu trữ ở đó, bạn không có quyền truy cập vào các tệp httpd.conf và chúng chưa thiết lập các miền dưới dạng alias của tên miền chính (mỗi tên miền có một thư mục riêng), sau đó bạn sẽ viết các quy tắc của mình trong thư mục gốc .htaccess cho www.mywebsite.com như này:

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] 

overhead Hầu hết

Nếu họ đang sử dụng bí danh (nơi mọi thứ đều có gốc cùng một tài liệu chính xác) sau đó bạn sẽ cần phải kiểm tra tên máy yêu cầu với một tập tin .htaccess đó là thường được ban hành bởi tất cả:

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] 
Các vấn đề liên quan