Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

add user agent



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?


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

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

welcome to WebmasterWorld, chitech!

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


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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

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

and so on.

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


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)


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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

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:


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".


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.

Featured Threads

Hot Threads This Week

Hot Threads This Month