Using Apache Environment Variables in .htaccess files with RewriteRule, SetEnvIf, Header, etc., is made easier by having a script that displays current variables and environment. Heres my slightly improved version of the common printenv and test-cgi scripts that come with Apache Installations.

Table of Contents

  1. Introduction
    1. Features of AskApache Environment Debugger
    2. Example Output and Variables
    3. The Shell Script

Features of AskApache Environment Debugger

  • Only displays results for user-configured IP address
  • If IP is not allowed it will redirect to any page you specify.
  • Can also be run from the shell
  • Includes both built-in env and also uses perl

Example Output and Variables

  • DOCUMENT_ROOT
  • GATEWAY_INTERFACE
  • HTTP_ACCEPT
  • HTTP_ACCEPT_CHARSET
  • HTTP_ACCEPT_ENCODING
  • HTTP_ACCEPT_LANGUAGE
  • HTTP_CACHE_CONTROL
  • HTTP_CONNECTION
  • HTTP_COOKIE
  • HTTP_HOST
  • HTTP_KEEP_ALIVE
  • HTTP_USER_AGENT
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_PORT
  • REQUEST_METHOD
  • REQUEST_URI
  • SCRIPT_FILENAME
  • SCRIPT_NAME
  • SCRIPT_URI
  • SCRIPT_URL
  • SERVER_ADDR
  • SERVER_ADMIN
  • SERVER_NAME
  • SERVER_PORT
  • SERVER_PROTOCOL
  • SERVER_SIGNATURE
  • SERVER_SOFTWARE

The Shell Script

Or Download

#!/bin/sh
###
### ASKAPACHE ENVIRONMENT DEBUGGER - Display Environment Variables using perl and builtin commands
### Version 1.0.2
###
### ---------------------------------------------------------------------------------------
### This work is licensed under the Creative Commons Attribution 3.0 United States License.
### To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/
### or send a letter to Creative Commons, 171 Second Street, Suite 300,
### San Francisco, California, 94105, USA.
### See https://www.askapache.com/about/disclaimer-and-license/ for info.
### ----------------------------------------------------------------------------------------
###
### Usage:
###
###   Place in public web directory request from your web browser.
###
###
### Install:
###
###   1. Place in public web directory like /cgi-bin/ and rename askapache-env.cgi
###   2. chmod 744 or chmod u+x
###   3. Add Options +ExecCGI to the top of /cgi-bin/.htaccess which should be chmod 644
###   4. Add your IP address to the MY_ALLOWED_IP variable in the config below (https://www.askapache.com/online-tools/whoami/)
###
###   Optionally you can add these lines to your .htaccess (https://www.askapache.com/htaccess/)
###   file to only allow from IP Address 208.113.183.103
###
###   Order Deny,Allow
###   Deny from All
###   Allow from 208.113.183.103


### CONFIGURATION
MY_ALLOWED_IP="208.113.183.103"
MY_REDIRECT="www.askapache.com"

# command line
if [ -e $GATEWAY_INTERFACE ]; then

echo "+ ARGS: ${0}"

echo "+-----------------------------------------------------+"
echo "| env                                                 |";
echo "+=====================================================+";
env | sort;echo;echo

echo "+-----------------------------------------------------+"
echo "| PERL PRINTENV                                       |";
echo "+=====================================================+";
perl -e'foreach $var (sort(keys(%ENV))) {$val = $ENV{$var};$val =~ s|n|\n|g;$val =~ s|"|\"|g;print "${var}="${val}"n"}';echo;echo

echo "+-----------------------------------------------------+"
echo "| declare                                             |";
echo "+=====================================================+";
declare | sort;echo;echo

else

if [ "$REMOTE_ADDR" == "$MY_ALLOWED_IP" ]; then

echo "Content-type: text/plain; charset=iso-8859-1";echo

echo "+ ARGS: ${0}"

echo "+-----------------------------------------------------+"
echo "| env                                                 |";
echo "+=====================================================+";
env | sort;echo;echo

echo "+-----------------------------------------------------+"
echo "| PERL PRINTENV                                       |";
echo "+=====================================================+";
perl -e'foreach $var (sort(keys(%ENV))) {$val = $ENV{$var};$val =~ s|n|\n|g;$val =~ s|"|\"|g;print "${var}="${val}"n"}';echo;echo

#echo "+-----------------------------------------------------+"
#echo "| declare                                             |";
#echo "+=====================================================+";
#declare | sort;echo;echo

else

echo "Content-type: text/html"
echo "Location: http://${MY_REDIRECT}";
echo "";

fi

fi

exit 0

Apache Environment Variables CGI Script - AskApache

Charles Torvalds
7 Jan 2008

  • Site Map WireShark GNU Non-GNU Tor Project cURL TLDP - Documentation
  • Htaccess Files Hacking Htaccess Javascript Linux Optimization PHP Security Shell Scripting WordPress
  • Base64 Image Converter Raw HTTP Header Debugger Graphical ASCII Text Generator Mac Address Vendor Lookup Who Am I – Your IP Information Request Method Security Scanner .htpasswd file Generator Compress CSS DNS Tracer
Copyright © 2025 AskApache
  • Site Map
  • Htaccess Files
  • Hacking
  • Htaccess
  • Javascript
  • Linux
  • Optimization
  • PHP
  • Security
  • Shell Scripting
  • WordPress
  • Base64 Image Converter
  • Raw HTTP Header Debugger
  • Graphical ASCII Text Generator
  • Mac Address Vendor Lookup
  • Who Am I – Your IP Information
  • Request Method Security Scanner
  • .htpasswd file Generator
  • Compress CSS
  • DNS Tracer
Exit mobile version