Home > database >  Htaccess entry for blocking user agent not working
Htaccess entry for blocking user agent not working

Time:10-06

The user agent blocking code in my .htaccess file stopped working, though I didn't make any recent changes to it. To troubleshoot this I changed the user agent in FF and created a sub-directory with its own .htaccess file and one php file. In that file I added code to display the $_SERVER['USER_AGENT'] string. It shows the UA I am using and is:

    Mozilla/5.0 (compatible; Konqueror/4.2; Linux) KHTML/4.2.96 (like Gecko)

In my .htaccess file I have the following but when I visit that location I am not blocked.

    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} "^Konqueror$" [NC]
    RewriteRule ^ - [F]

I added the following, with my real IP, after the above to verify the rewrite was working and it blocked me:

    RewriteCond %{REMOTE_ADDR} ^12\.23\.45\.666
    RewriteRule ^ - [F]

Does anyone know what the problem is?

CodePudding user response:

This won't work due to the "^" at the start and the "$" at the end of your matching pattern in the RewriteCond. These two special characters anchor a regular expression to the beginning and the end of the subject, so the search string. So your condition will only match of the header contains the exact string "Konqueror" with nothing before or after that. That is not what you want.

The word "Konqueror" appears in the middle of that string you send as a user agent. So you want to match any string that contains the word "Konqueror" most likely. Just leave both special characters away and you are happy.

You can also remove the quote characters, they are not required.

So simply use that condition:

RewriteCond %{HTTP_USER_AGENT} Konqueror [NC]
  • Related