Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

setenv custom error page



9:39 pm on Mar 27, 2012 (gmt 0)

I've searched all over for this but unsuccessfully.

Can a custom error page be specified at the end of a set or 'SetEnv[IfNoCase]' deny rules or are they automatically served the custom 403 ?

I don't wish to use my current 403 page but serve a SetEnv rule-specific page.

SetEnvIfNoCase User-Agent "aaaaa" bad_bot
SetEnvIfNoCase User-Agent "bbbbb" bad_bot
Order Deny,Allow
Deny from env=bad_bot
serve: custom-block.php

Thank you in advance


10:16 pm on Mar 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

You can do this easily with mod_rewrite but using SetEnvIf gets a little more complicated (or not depending on your view).

When you set an environmental variable in .htaccess, your custom-block.php script has access to that same variable and its value. So in your script, you would need some code that would work check for that variable:

if variable bad_bot=1 { do something; }


11:17 pm on Mar 27, 2012 (gmt 0)

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

You can definitely do it with a redirect, although that might be considered cheating. That is: redirect your Bad Files to a special directory, and set a different 403 document for that one directory. That's assuming they follow the redirect, which they might choose not to do. But if they're unwanted robots, you probably don't care if they just leave quietly.

Here's another thing that will not work (I tried) ;)

If you set more than one ErrorDocument, like
ErrorDocument 403 blahblah
ErrorDocument 403 blahblah
the most recent one will quietly replace any earlier ones. But if you block your Bad Requests from the most recent ErrorDocument, they will not bounce back to the earlier ErrorDocument. Instead you'll get an error message from the server.


7:34 am on Mar 28, 2012 (gmt 0)

Plenty of food for thought.
Many thanks for your replies guys ('n' gal).


8:24 pm on Mar 29, 2012 (gmt 0)

Guys - Lucy specifically - if I understood you correctly, what's the actual method of specifying a different error page to your regular 403 in htaccess for your SetEnv rules please? I'm wanting to give the redirect method a try first but am unsure how to action it in htaccess.
Thanks in advance


10:43 pm on Mar 29, 2012 (gmt 0)

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

With the redirect method you don't use SetEnv at all. (I looked pretty hard, as I assume you did, but couldn't find an env= envelope. Just Files and Directory, and I forget the third.) You put an htaccess in the Directory For Icky Robots, with its own

ErrorDocument blahblah

statement. For example: my top-level htaccess has a series of

ErrorDocument 404 /boilerplate/missing.html

for the assorted common errors. Then, in the /dp/ directory I've got a one-line htaccess that says

ErrorDocument 404 /dp/missing.html

If someone's in this directory at all, they are probably following some ancient Forums link that I've forgotten all about, so they get customized information.

A directory-specific ErrorDocument statement overrides your generic rules in exactly the same way that you can switch auto-indexing off or on for individual directories. It will be inherited by subdirectories-- if there are any-- but will have no effect on other directories.


11:18 am on Mar 30, 2012 (gmt 0)

Understood. I'll give this a go and see if it's better than other methods.
Thank you Lucy.