Search code examples
apache.htaccessgooglebot

Verifying Googlebot in .htaccess file


I have been investigate a bit. Will code below work? Not so easy to check.

RewriteEngine on
HostnameLookups Double  
RewriteCond %{REMOTE_HOST} (\.googlebot\.com) [NC] 
RewriteRule ^(.*)$ /do-something [L,R]

I worry the most for part

HostnameLookups Double 

It says in some place that works only in httpd.confg, vps, directory(not shure what this last means if not .htaccess but not saying in htaccess). Do you have knowledge about this issue?


Solution

  • You can use a condition with %{HTTP_USER_AGENT} variable:

    RewriteEngine on
    
    RewriteCond %{HTTP_USER_AGENT} ^googlebot
    RewriteRule ^(.*)$ /do-something [L,R]
    

    Though keep in mind that %{HTTP_USER_AGENT} can be spoofed.