2017-09-26 22 views
0

的.htaccess允許只有當提出要求機器人圖像直接URL訪問以其它方式使用的.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

我在做什麼通過URL來index.php文件正在處理所有圖像網址的index.php並運行下面的條件。如果發現機器人(googlebot,bingbot等)然後顯示圖像,否則運行一些東西。現在我想檢測使用.htaccess的機器人,並允許圖像直接打開繞過php部分。

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

我對網絡編程很陌生。經過很多搜索後,我找不到任何適合我的信息。這是我的第一個問題stackoverflow。感謝您的幫助

回答

0

我指基於以下_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行爲,有一個高度變化,這個匹配的標準沒有找到所有發現機器人。

相關問題