homepage Welcome to WebmasterWorld Guest from 54.205.99.71
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
add user agent
chitech




msg:4603230
 11:20 am on Aug 19, 2013 (gmt 0)

How can I add a custom user agent "none-bot" for request that does not contains an user agent header?

 

phranque




msg:4603236
 11:49 am on Aug 19, 2013 (gmt 0)

welcome to WebmasterWorld, chitech!


you want a custom user agent in the server access log?

lucy24




msg:4603245
 11:59 am on Aug 19, 2013 (gmt 0)

Are you talking about doing something to robots who come in with a blank user-agent?

^-?$

"The user-agent string consists of exactly - or exactly nothing at all."

In practice it's probably "exactly nothing", but by the time it reaches your logs it will generally be rendered as "-". So you may as well cover yourself both ways.

In mod_setenvif:
BrowserMatch ^-?$

In mod_rewrite
%{HTTP_USER_AGENT} ^-?$

and so on.

Or do you mean that you want to change the incoming request header? I kinda think it can't be done.

chitech




msg:4603249
 12:20 pm on Aug 19, 2013 (gmt 0)

I have solved it with this:

SetEnvIfNoCase User-Agent .+ nouseragent
RequestHeader set User-Agent 'none-bot' env=!nouseragent

The only problem is that is catch both "-" (no User-Agent Header)and "" (empty User-Agent)

lucy24




msg:4603315
 4:16 pm on Aug 19, 2013 (gmt 0)

Gosh. I've never met mod_headers before-- and can't for the life of me figure out what RequestHeader is supposed to be for! (That is, if a process can read headers in the first place, why can't it read "^-?$" just as well as "none-bot"?)

SetEnvIfNoCase User-Agent

mod_setenvif has a useful shorthand just for user-agents:

BrowserMatch
BrowserMatchNoCase

I don't see how your code does what you say. (Although I hadn't realized that the difference between "-" and "" is "missing" vs. "empty".)

The form .+ captures one or more characters, including a single - or exactly the form you don't want to catch. You could say .{2,} assuming no legitimate user-agent string will consist of only one character. If a robot really did come in introducing itself as "a", well, it deserves what it gets!

I would also suggest that for your own sake you should call the environmental variable something else. Anyone looking at the code-- including yourself in a few months' time--would say "No, that's backward, the form .+ means there is a user-agent".

Can't you achieve the same thing in a single line using
RequestHeader edit
?
RequestHeader set|append|merge|add|unset|edit header [value] [replacement]

"value" here would be ^$ (empty) and/or ^-$ (missing), while "replacement" is "none-bot".

chitech




msg:4603548
 11:44 am on Aug 20, 2013 (gmt 0)

Hi Lucy24

You are right it should have been:

SetEnvIfNoCase User-Agent .+ hasuseragent
RequestHeader set User-Agent 'none-bot' env=!hasuseragent

I didn't know "RequestHeader edit" have a [replacement] field. Thx for the tips.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved