簡體   English   中英

僅當bot要求時才允許直接url訪問圖像,否則使用.htaccess將url傳遞到index.php文件

[英]allow direct url access to image only if requested by bot otherwise pass url to index.php file using .htaccess

.htaccess

#Disallowed
#RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
#RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
#RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]

#Allowed
RewriteCond %{REQUEST_URI}  !=/index.php
RewriteCond %{REQUEST_URI}  !.*\.php$ [NC]
RewriteCond %{REQUEST_URI}  !.*\.css$ [NC]
RewriteCond %{REQUEST_URI}  !.*\.js$ [NC]
RewriteRule . /index.php [L]

index.php

我正在做的是使用index.php處理所有圖像url並運行以下條件。 如果檢測到機器人(googlebot,bingbot等),則顯示圖像,否則運行某些程序。 現在,我想使用.htaccess檢測bot並允許圖像繞過php部分直接打開。

if(_bot_detected()){
  //echo image
}else{
  //redirect user to some page
}

我是Web編程的新手。 經過大量搜索,我找不到適合我的任何信息。 這是我對stackoverflow的第一個問題。 謝謝你的幫助

我根據以下_bot_detected()函數引用我的答案:
https://gist.github.com/holgerhubbs/983a53ee21a9f6bff08d
如果您的功能不同,則匹配將不是100%相等。

我們唯一需要檢查的是請求是否需要圖像而不是來自機器人,因為我們必須重寫:

# Check if the URI is a physical file
RewriteCond %{REQUEST_URI} -f
# Check if the file has any kind off Image extension like:
# .gif .jpg .jpeg .tif .tiff .png .svg .bmp (case insensitive => [NC])
RewriteCond %{REQUEST_URI} \.(gif|jpe?g|tiff?|png|svg|bmp)$ [NC]
# check if the request in NOT coming from a bot
RewriteCond %{HTTP_USER_AGENT} !(bot|crawl|slurp|spider) [NC]
# If all Condition are satisfied than we rewrite to /index.php
RewriteRule ^.*$ /index.php [L]

甚至我的答案也應該與您原來的php行為相匹配,但是這種匹配標准並沒有找到所有找到的bot,這是一個高度變化。

暫無
暫無

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

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