簡體   English   中英

configure僅允許特定域使用.htaccess訪問某些文件夾

[英]configure only allow specific domains to access certain folders using .htaccess

我的情況如下:

例如,我有一個網站http://www.example.com ,我已經設置了一些子域名,例如http://image2.example.com http://video.example.comhttp://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文件配置它?

將此代碼放在/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]

您可以檢查主機,然后使用mod_rewrite.htaccess文件中創建301重定向來處理它; 雖然如果你有權訪問,那么如果你在httpd.conf或包含的配置文件中執行此操作會好得多。

最佳場景

由於看起來mod_cband想要為每個域使用不同的虛擬主機 ,因此您可以設置類似於此的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 (每個域都有一個單獨的文件夾),那么您會在你的www.mywebsite.com根目錄.htaccess編寫規則,如下所示:

  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]
rewritecond %{HTTP_HOST} video.mywebsite.com [s=1]
rewriterule files/videos/.* - [F]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM