<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AskApache &#187; Search Results  &#187;  feedcount</title>
	<atom:link href="http://www.askapache.com/search/feedcount/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.askapache.com</link>
	<description>Advanced Web Development</description>
	<lastBuildDate>Thu, 26 Apr 2012 11:29:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Crazy Advanced Mod_Rewrite Debug Tutorial</title>
		<link>http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html</link>
		<comments>http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html#comments</comments>
		<pubDate>Fri, 11 Sep 2009 17:55:32 +0000</pubDate>
		<dc:creator>AskApache</dc:creator>
				<category><![CDATA[Htaccess]]></category>

		<guid isPermaLink="false">http://www.askapache.com/?p=1209</guid>
		<description><![CDATA[<p><strong>Note:  Extremely ILL Content</strong><br />Find the key to unlocking mod_rewrite and you WILL be sick..  sick with a diamond disease on your wrist!<br /><a class="hs hs13" href="http://uploads.askapache.com/2008/08/mod_rewrite_ascii.png"></a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html"></a><a href="http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html"><cite>AskApache.com</cite></a></p><p><a class="hs hs13" href="http://uploads.askapache.com/2008/08/mod_rewrite_ascii.png"></a><strong>Are you an advanced mod_rewrite expert or guru?</strong>  This article is for YOU too!  Just make sure to read all the way to the bottom..<br class="C" /></p>

<p class="anote">The following undocumented techniques and methods will allow you to utilize mod_rewrite at an  "expert level" by showing you how to <a href="http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html#decoded">unlock its secrets</a>.  <br class="C" /></p>


<p>Most if not all web developers and server administrators struggle with Apache mod_rewrite.  It's very tough and only gets a little easier with practice.  Until Now!  Get ready to explode your learning curve, I figured something out.</p>


<h2>Why mod_rewrite is so tough</h2>
<p>I have come to the conclusion, after many hours of zenful thought, that the reason mod_rewrite is so tough is pretty obvious, people are trying to apply regular-expressions to URLs and Variables that they don't really understand.  They understand what they want, but they don't understand what the URLS and Variables are that they are trying to rewrite.</p>

<h2>Hit-Or-Miss with mod_rewrite</h2>
<p>A lot of the mod_rewrite "experts" and "gurus" floating around the net absolutely know their mod_rewrite, but what separates them from a beginner or novice is for the most part an understanding of what the URLS and Variables look like that are targeted by the regular expressions.  Take this simple rewriterule that rewrites requests made without the www to www.</p>
<pre>
RewriteEngine On
RewriteBase /
&nbsp;
RewriteCond %{HTTP_HOST} !^www\.askapache\.com$ [NC]
RewriteRule .+ http://www.askapache.com%{REQUEST_URI}
</pre>

<p>Pretty simple right?  WRONG.  Most people could not figure that out..</p>

<h3>Why?</h3>
<p>The reason intelligent people can't figure that out is because they have no idea what HTTP_HOST or REQUEST_URI actually looks like.  How can you write a rule for something if you don't know what it looks like?  You can't.</p>

<h2>When Not To Use Mod_Rewrite</h2>
<p>Ok so heres an important concept that alot of people haven't heard.  You should only use <a href="http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html">mod_rewrite's rewriterule </a>when you use a rewritecond or if you are rewriting internally like my <a href="http://www.askapache.com/htaccess/pimp-out-your-feedburner-count.html">feedcount hack</a>.</p>
<p>If you are simply redirecting one url to another, you should definately be using the much easier <a href="http://www.askapache.com/htaccess/seo-search-engine-friendly-redirects-without-mod_rewrite.html">mod_alias's redirect and redirectmatch</a>, which is enabled on most Apache servers.</p>


<h2>When To Use Mod_Rewrite</h2>
<p>So then, you should only use mod_rewrite's rewriterule when you are checking against one of the Apache Environment Variables to determine whether to rewrite or not.  This is where the Apache Documentation is grossly lacking.  They don't tell you what those variables look like, leaving us completely incapable of creating rewrites based on them.  <strong>Not anymore.</strong></p>

<h3>Mod_Rewrite Environment Variables (The Secret)</h3>
<p>Here's the variables I have found accessible by <a href="http://httpd.apache.org/docs/trunk/mod/mod_rewrite.html#rewritecond">mod_rewrite</a> (both documented and undocumented).   A thing to note is that you can set these variables early in an .htaccess file using SetEnv, RewriteRule, Header, etc.. and they will be accessible at the end of the .htaccess file.<br class="C" /></p>

<table class="bordered"><tbody><tr class="header"><th>Name</th></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#API_VERSION"><code>API_VERSION</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#AUTH_TYPE"><code>AUTH_TYPE</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#CONTENT_LENGTH"><code>CONTENT_LENGTH</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#CONTENT_TYPE"><code>CONTENT_TYPE</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#DOCUMENT_ROOT"><code>DOCUMENT_ROOT</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#GATEWAY_INTERFACE"><code>GATEWAY_INTERFACE</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTPS"><code>HTTPS</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_ACCEPT"><code>HTTP_ACCEPT</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_ACCEPT_CHARSET"><code>HTTP_ACCEPT_CHARSET</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_ACCEPT_ENCODING"><code>HTTP_ACCEPT_ENCODING</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_ACCEPT_LANGUAGE"><code>HTTP_ACCEPT_LANGUAGE</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_CACHE_CONTROL"><code>HTTP_CACHE_CONTROL</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_CONNECTION"><code>HTTP_CONNECTION</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_COOKIE"><code>HTTP_COOKIE</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_FORWARDED"><code>HTTP_FORWARDED</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_HOST"><code>HTTP_HOST</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_KEEP_ALIVE"><code>HTTP_KEEP_ALIVE</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_PROXY_CONNECTION"><code>HTTP_PROXY_CONNECTION</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_REFERER"><code>HTTP_REFERER</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_USER_AGENT"><code>HTTP_USER_AGENT</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#IS_SUBREQ"><code>IS_SUBREQ</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#ORIG_PATH_INFO"><code>ORIG_PATH_INFO</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#ORIG_PATH_TRANSLATED"><code>ORIG_PATH_TRANSLATED</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#ORIG_SCRIPT_FILENAME"><code>ORIG_SCRIPT_FILENAME</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#ORIG_SCRIPT_NAME"><code>ORIG_SCRIPT_NAME</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#PATH"><code>PATH</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#PATH_INFO"><code>PATH_INFO</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#PHP_SELF"><code>PHP_SELF</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#QUERY_STRING"><code>QUERY_STRING</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REDIRECT_QUERY_STRING"><code>REDIRECT_QUERY_STRING</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REDIRECT_REMOTE_USER"><code>REDIRECT_REMOTE_USER</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REDIRECT_STATUS"><code>REDIRECT_STATUS</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REDIRECT_URL"><code>REDIRECT_URL</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REMOTE_ADDR"><code>REMOTE_ADDR</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REMOTE_HOST"><code>REMOTE_HOST</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REMOTE_IDENT"><code>REMOTE_IDENT</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REMOTE_PORT"><code>REMOTE_PORT</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REMOTE_USER"><code>REMOTE_USER</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REQUEST_FILENAME"><code>REQUEST_FILENAME</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REQUEST_METHOD"><code>REQUEST_METHOD</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REQUEST_TIME"><code>REQUEST_TIME</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#REQUEST_URI"><code>REQUEST_URI</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SCRIPT_FILENAME"><code>SCRIPT_FILENAME</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SCRIPT_GROUP"><code>SCRIPT_GROUP</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SCRIPT_NAME"><code>SCRIPT_NAME</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SCRIPT_URI"><code>SCRIPT_URI</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SCRIPT_URL"><code>SCRIPT_URL</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SCRIPT_USER"><code>SCRIPT_USER</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SERVER_ADDR"><code>SERVER_ADDR</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SERVER_ADMIN"><code>SERVER_ADMIN</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SERVER_NAME"><code>SERVER_NAME</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SERVER_PORT"><code>SERVER_PORT</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SERVER_PROTOCOL"><code>SERVER_PROTOCOL</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SERVER_SIGNATURE"><code>SERVER_SIGNATURE</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SERVER_SOFTWARE"><code>SERVER_SOFTWARE</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_CIPHER"><code>SSL_CIPHER</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_CIPHER_ALGKEYSIZE"><code>SSL_CIPHER_ALGKEYSIZE</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_CIPHER_EXPORT"><code>SSL_CIPHER_EXPORT</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_CIPHER_USEKEYSIZE"><code>SSL_CIPHER_USEKEYSIZE</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_CLIENT_VERIFY"><code>SSL_CLIENT_VERIFY</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_PROTOCOL"><code>SSL_PROTOCOL</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_A_KEY"><code>SSL_SERVER_A_KEY</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_A_SIG"><code>SSL_SERVER_A_SIG</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_CERT"><code>SSL_SERVER_CERT</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_I_DN"><code>SSL_SERVER_I_DN</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_I_DN_C"><code>SSL_SERVER_I_DN_C</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_I_DN_CN"><code>SSL_SERVER_I_DN_CN</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_I_DN_L"><code>SSL_SERVER_I_DN_L</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_I_DN_O"><code>SSL_SERVER_I_DN_O</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_I_DN_OU"><code>SSL_SERVER_I_DN_OU</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_I_DN_ST"><code>SSL_SERVER_I_DN_ST</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_M_SERIAL"><code>SSL_SERVER_M_SERIAL</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_M_VERSION"><code>SSL_SERVER_M_VERSION</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_S_DN"><code>SSL_SERVER_S_DN</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_S_DN_CN"><code>SSL_SERVER_S_DN_CN</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_S_DN_O"><code>SSL_SERVER_S_DN_O</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_S_DN_OU"><code>SSL_SERVER_S_DN_OU</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_V_END"><code>SSL_SERVER_V_END</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SERVER_V_START"><code>SSL_SERVER_V_START</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_SESSION_ID"><code>SSL_SESSION_ID</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_VERSION_INTERFACE"><code>SSL_VERSION_INTERFACE</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#SSL_VERSION_LIBRARY"><code>SSL_VERSION_LIBRARY</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#THE_REQUEST"><code>THE_REQUEST</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#TIME"><code>TIME</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#TIME_DAY"><code>TIME_DAY</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#TIME_HOUR"><code>TIME_HOUR</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#TIME_MIN"><code>TIME_MIN</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#TIME_MON"><code>TIME_MON</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#TIME_SEC"><code>TIME_SEC</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#TIME_WDAY"><code>TIME_WDAY</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#TIME_YEAR"><code>TIME_YEAR</code></a></td></tr>
<tr><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#TZ"><code>TZ</code></a></td></tr>
<tr class="odd"><td><a href="/htaccess/mod_rewrite-variables-cheatsheet.html#UNIQUE_ID"><code>UNIQUE_ID</code></a></td></tr>
</tbody></table>


<hr class="C" />



<h2>Decoding Mod_Rewrite Variables</h2>
<p>So when I realized my problem was that I didn't know the value of the variable being tested by the RewriteCond, I set out to try and discover how to view those variables..  Keep in mind you can also use RewriteLogging, but its only allowed for root users who can edit the httpd.conf, this is .htaccess.</p>

<h3>Setting Environment Variables with RewriteRule</h3>
<p>I discovered a multitude of methods to set and view apache environment variables, using various modules and some core tricks, but the method that allows me to view the most environment variables is RewriteRule.. I wanted to use <a href="http://www.askapache.com/htaccess/setenvif.html">SetEnvIf</a> more, but its just not as powerful as mod_rewrite, due to programming.</p>

<p>This code sets the variable <code>INFO_REQUEST_URI</code> to have the value of <code>REQUEST_URI</code>.</p>
<pre>
RewriteEngine On
RewriteBase /
RewriteRule .* - [E=INFO_REQUEST_URI:%{REQUEST_URI},NE]
</pre>



<h3>Saving the Apache Variable Values</h3>
<p>Now the trick is how to view that environment variable...  The method I came up with is nice... We will send the environment variable value in an HTTP Header, as there isn't much data manipulation/validation so you get an accurate look at the actual value..  At first I tried adding the variable value to a redirection using the query_string.. but a HTTP_USER_AGENT value doesn't play well as a query_string.</p>

<h4>Using RequestHeader in .htaccess</h4>
<p>This code takes advantage of the incredible mod_headers apache module to actually ADD a whole new header to YOUR request.  Seriously one of the coolest tricks I've found yet.. Its almost the same as being able to spoof POST requests!  Since Headers can be protected data... <em>especially the HTTP_COOKIE header</em>..</p>
<pre>
RequestHeader set INFO_REQUEST_URI "%{INFO_REQUEST_URI}e"
</pre>


<h3>Viewing the Variable Values</h3>
<p>Now you can use any kind of server-run interpreter like perl, php, ruby, etc., to view all the variable values.  All cgi-script handlers like those are able to view request headers.. </p>

<h3>PHP Code to access Apache Variables</h3>
<p>Works even in safe-mode... any interpreter can view HTTP Headers!  Note that each of these variables are added as HTTP headers to the request for the script.. kinda confusing.. So each variable sent as a header is prefixed with HTTP_ to denote it was a header.</p>

<pre>
&lt;?php
header("Content-Type: text/plain");
$INFO=$MISS=array();
foreach($_SERVER as $v=&gt;$r)
{
  if(substr($v,0,9)==&#039;HTTP_INFO&#039;)
  {
    if(!empty($r))$INFO[substr($v,10)]=$r;
    else $MISS[substr($v,10)]=$r;
  }
}
&nbsp;
/* thanks Mike! */
ksort($INFO);
ksort($MISS);
ksort($_SERVER);
&nbsp;
echo "Received These Variables:\n";
print_r($INFO);
&nbsp;
echo "Missed These Variables:\n";
print_r($MISS);
&nbsp;
echo "ALL Variables:\n";
print_r($_SERVER);
?&gt;
</pre>



<h2>Time to Get Crazy</h2>
<p>Just create the above php file on your site as <code>/test/index.php</code> or whatever, then create /test/.htaccess which should contain the below <code>.htaccess file</code> snippet.  Now just request <code>/test/index.php</code> and be amazed!   If you're looking for more general help check out this excellent <a href="http://www.addedbytes.com/apache/mod_rewrite-cheat-sheet/">mod_rewrite cheat sheet</a>.</p>

<p>Ok, so I've prepared the .htaccess code you can use to view the values of all these variables.  Just add it to a .htaccess file and make a request.  For this test I created an index.php file that printed out all the <code>$_SERVER</code> variables, and made requests to it.</p>



<pre>
RewriteEngine On
RewriteBase /
RewriteRule .* - [E=INFO_API_VERSION:%{API_VERSION},NE]
RewriteRule .* - [E=INFO_AUTH_TYPE:%{AUTH_TYPE},NE]
RewriteRule .* - [E=INFO_CONTENT_LENGTH:%{CONTENT_LENGTH},NE]
RewriteRule .* - [E=INFO_CONTENT_TYPE:%{CONTENT_TYPE},NE]
RewriteRule .* - [E=INFO_DOCUMENT_ROOT:%{DOCUMENT_ROOT},NE]
RewriteRule .* - [E=INFO_GATEWAY_INTERFACE:%{GATEWAY_INTERFACE},NE]
RewriteRule .* - [E=INFO_HTTPS:%{HTTPS},NE]
RewriteRule .* - [E=INFO_HTTP_ACCEPT:%{HTTP_ACCEPT},NE]
RewriteRule .* - [E=INFO_HTTP_ACCEPT_CHARSET:%{HTTP_ACCEPT_CHARSET},NE]
RewriteRule .* - [E=INFO_HTTP_ACCEPT_ENCODING:%{HTTP_ACCEPT_ENCODING},NE]
RewriteRule .* - [E=INFO_HTTP_ACCEPT_LANGUAGE:%{HTTP_ACCEPT_LANGUAGE},NE]
RewriteRule .* - [E=INFO_HTTP_CACHE_CONTROL:%{HTTP_CACHE_CONTROL},NE]
RewriteRule .* - [E=INFO_HTTP_CONNECTION:%{HTTP_CONNECTION},NE]
RewriteRule .* - [E=INFO_HTTP_COOKIE:%{HTTP_COOKIE},NE]
RewriteRule .* - [E=INFO_HTTP_FORWARDED:%{HTTP_FORWARDED},NE]
RewriteRule .* - [E=INFO_HTTP_HOST:%{HTTP_HOST},NE]
RewriteRule .* - [E=INFO_HTTP_KEEP_ALIVE:%{HTTP_KEEP_ALIVE},NE]
RewriteRule .* - [E=INFO_HTTP_MOD_SECURITY_MESSAGE:%{HTTP_MOD_SECURITY_MESSAGE},NE]
RewriteRule .* - [E=INFO_HTTP_PROXY_CONNECTION:%{HTTP_PROXY_CONNECTION},NE]
RewriteRule .* - [E=INFO_HTTP_REFERER:%{HTTP_REFERER},NE]
RewriteRule .* - [E=INFO_HTTP_USER_AGENT:%{HTTP_USER_AGENT},NE]
RewriteRule .* - [E=INFO_IS_SUBREQ:%{IS_SUBREQ},NE]
RewriteRule .* - [E=INFO_ORIG_PATH_INFO:%{ORIG_PATH_INFO},NE]
RewriteRule .* - [E=INFO_ORIG_PATH_TRANSLATED:%{ORIG_PATH_TRANSLATED},NE]
RewriteRule .* - [E=INFO_ORIG_SCRIPT_FILENAME:%{ORIG_SCRIPT_FILENAME},NE]
RewriteRule .* - [E=INFO_ORIG_SCRIPT_NAME:%{ORIG_SCRIPT_NAME},NE]
RewriteRule .* - [E=INFO_PATH:%{PATH},NE]
RewriteRule .* - [E=INFO_PATH_INFO:%{PATH_INFO},NE]
RewriteRule .* - [E=INFO_PHP_SELF:%{PHP_SELF},NE]
RewriteRule .* - [E=INFO_QUERY_STRING:%{QUERY_STRING},NE]
RewriteRule .* - [E=INFO_REDIRECT_QUERY_STRING:%{REDIRECT_QUERY_STRING},NE]
RewriteRule .* - [E=INFO_REDIRECT_REMOTE_USER:%{REDIRECT_REMOTE_USER},NE]
RewriteRule .* - [E=INFO_REDIRECT_STATUS:%{REDIRECT_STATUS},NE]
RewriteRule .* - [E=INFO_REDIRECT_URL:%{REDIRECT_URL},NE]
RewriteRule .* - [E=INFO_REMOTE_ADDR:%{REMOTE_ADDR},NE]
RewriteRule .* - [E=INFO_REMOTE_HOST:%{REMOTE_HOST},NE]
RewriteRule .* - [E=INFO_REMOTE_IDENT:%{REMOTE_IDENT},NE]
RewriteRule .* - [E=INFO_REMOTE_PORT:%{REMOTE_PORT},NE]
RewriteRule .* - [E=INFO_REMOTE_USER:%{REMOTE_USER},NE]
RewriteRule .* - [E=INFO_REQUEST_FILENAME:%{REQUEST_FILENAME},NE]
RewriteRule .* - [E=INFO_REQUEST_METHOD:%{REQUEST_METHOD},NE]
RewriteRule .* - [E=INFO_REQUEST_TIME:%{REQUEST_TIME},NE]
RewriteRule .* - [E=INFO_REQUEST_URI:%{REQUEST_URI},NE]
RewriteRule .* - [E=INFO_SCRIPT_FILENAME:%{SCRIPT_FILENAME},NE]
RewriteRule .* - [E=INFO_SCRIPT_GROUP:%{SCRIPT_GROUP},NE]
RewriteRule .* - [E=INFO_SCRIPT_NAME:%{SCRIPT_NAME},NE]
RewriteRule .* - [E=INFO_SCRIPT_URI:%{SCRIPT_URI},NE]
RewriteRule .* - [E=INFO_SCRIPT_URL:%{SCRIPT_URL},NE]
RewriteRule .* - [E=INFO_SCRIPT_USER:%{SCRIPT_USER},NE]
RewriteRule .* - [E=INFO_SERVER_ADDR:%{SERVER_ADDR},NE]
RewriteRule .* - [E=INFO_SERVER_ADMIN:%{SERVER_ADMIN},NE]
RewriteRule .* - [E=INFO_SERVER_NAME:%{SERVER_NAME},NE]
RewriteRule .* - [E=INFO_SERVER_PORT:%{SERVER_PORT},NE]
RewriteRule .* - [E=INFO_SERVER_PROTOCOL:%{SERVER_PROTOCOL},NE]
RewriteRule .* - [E=INFO_SERVER_SIGNATURE:%{SERVER_SIGNATURE},NE]
RewriteRule .* - [E=INFO_SERVER_SOFTWARE:%{SERVER_SOFTWARE},NE]
RewriteRule .* - [E=INFO_THE_REQUEST:%{THE_REQUEST},NE]
RewriteRule .* - [E=INFO_TIME:%{TIME},NE]
RewriteRule .* - [E=INFO_TIME_DAY:%{TIME_DAY},NE]
RewriteRule .* - [E=INFO_TIME_HOUR:%{TIME_HOUR},NE]
RewriteRule .* - [E=INFO_TIME_MIN:%{TIME_MIN},NE]
RewriteRule .* - [E=INFO_TIME_MON:%{TIME_MON},NE]
RewriteRule .* - [E=INFO_TIME_SEC:%{TIME_SEC},NE]
RewriteRule .* - [E=INFO_TIME_WDAY:%{TIME_WDAY},NE]
RewriteRule .* - [E=INFO_TIME_YEAR:%{TIME_YEAR},NE]
RewriteRule .* - [E=INFO_TZ:%{TZ},NE]
RewriteRule .* - [E=INFO_UNIQUE_ID:%{UNIQUE_ID},NE]
&nbsp;
RequestHeader set INFO_API_VERSION "%{INFO_API_VERSION}e"
RequestHeader set INFO_AUTH_TYPE "%{INFO_AUTH_TYPE}e"
RequestHeader set INFO_CONTENT_LENGTH "%{INFO_CONTENT_LENGTH}e"
RequestHeader set INFO_CONTENT_TYPE "%{INFO_CONTENT_TYPE}e"
RequestHeader set INFO_DOCUMENT_ROOT "%{INFO_DOCUMENT_ROOT}e"
RequestHeader set INFO_GATEWAY_INTERFACE "%{INFO_GATEWAY_INTERFACE}e"
RequestHeader set INFO_HTTPS "%{INFO_HTTPS}e"
RequestHeader set INFO_HTTP_ACCEPT "%{INFO_HTTP_ACCEPT}e"
RequestHeader set INFO_HTTP_ACCEPT_CHARSET "%{INFO_HTTP_ACCEPT_CHARSET}e"
RequestHeader set INFO_HTTP_ACCEPT_ENCODING "%{INFO_HTTP_ACCEPT_ENCODING}e"
RequestHeader set INFO_HTTP_ACCEPT_LANGUAGE "%{INFO_HTTP_ACCEPT_LANGUAGE}e"
RequestHeader set INFO_HTTP_CACHE_CONTROL "%{INFO_HTTP_CACHE_CONTROL}e"
RequestHeader set INFO_HTTP_CONNECTION "%{INFO_HTTP_CONNECTION}e"
RequestHeader set INFO_HTTP_COOKIE "%{INFO_HTTP_COOKIE}e"
RequestHeader set INFO_HTTP_FORWARDED "%{INFO_HTTP_FORWARDED}e"
RequestHeader set INFO_HTTP_HOST "%{INFO_HTTP_HOST}e"
RequestHeader set INFO_HTTP_KEEP_ALIVE "%{INFO_HTTP_KEEP_ALIVE}e"
RequestHeader set INFO_HTTP_MOD_SECURITY_MESSAGE "%{INFO_HTTP_MOD_SECURITY_MESSAGE}e"
RequestHeader set INFO_HTTP_PROXY_CONNECTION "%{INFO_HTTP_PROXY_CONNECTION}e"
RequestHeader set INFO_HTTP_REFERER "%{INFO_HTTP_REFERER}e"
RequestHeader set INFO_HTTP_USER_AGENT "%{INFO_HTTP_USER_AGENT}e"
RequestHeader set INFO_IS_SUBREQ "%{INFO_IS_SUBREQ}e"
RequestHeader set INFO_ORIG_PATH_INFO "%{INFO_ORIG_PATH_INFO}e"
RequestHeader set INFO_ORIG_PATH_TRANSLATED "%{INFO_ORIG_PATH_TRANSLATED}e"
RequestHeader set INFO_ORIG_SCRIPT_FILENAME "%{INFO_ORIG_SCRIPT_FILENAME}e"
RequestHeader set INFO_ORIG_SCRIPT_NAME "%{INFO_ORIG_SCRIPT_NAME}e"
RequestHeader set INFO_PATH "%{INFO_PATH}e"
RequestHeader set INFO_PATH_INFO "%{INFO_PATH_INFO}e"
RequestHeader set INFO_PHP_SELF "%{INFO_PHP_SELF}e"
RequestHeader set INFO_QUERY_STRING "%{INFO_QUERY_STRING}e"
RequestHeader set INFO_REDIRECT_QUERY_STRING "%{INFO_REDIRECT_QUERY_STRING}e"
RequestHeader set INFO_REDIRECT_REMOTE_USER "%{INFO_REDIRECT_REMOTE_USER}e"
RequestHeader set INFO_REDIRECT_STATUS "%{INFO_REDIRECT_STATUS}e"
RequestHeader set INFO_REDIRECT_URL "%{INFO_REDIRECT_URL}e"
RequestHeader set INFO_REMOTE_ADDR "%{INFO_REMOTE_ADDR}e"
RequestHeader set INFO_REMOTE_HOST "%{INFO_REMOTE_HOST}e"
RequestHeader set INFO_REMOTE_IDENT "%{INFO_REMOTE_IDENT}e"
RequestHeader set INFO_REMOTE_PORT "%{INFO_REMOTE_PORT}e"
RequestHeader set INFO_REMOTE_USER "%{INFO_REMOTE_USER}e"
RequestHeader set INFO_REQUEST_FILENAME "%{INFO_REQUEST_FILENAME}e"
RequestHeader set INFO_REQUEST_METHOD "%{INFO_REQUEST_METHOD}e"
RequestHeader set INFO_REQUEST_TIME "%{INFO_REQUEST_TIME}e"
RequestHeader set INFO_REQUEST_URI "%{INFO_REQUEST_URI}e"
RequestHeader set INFO_SCRIPT_FILENAME "%{INFO_SCRIPT_FILENAME}e"
RequestHeader set INFO_SCRIPT_GROUP "%{INFO_SCRIPT_GROUP}e"
RequestHeader set INFO_SCRIPT_NAME "%{INFO_SCRIPT_NAME}e"
RequestHeader set INFO_SCRIPT_URI "%{INFO_SCRIPT_URI}e"
RequestHeader set INFO_SCRIPT_URL "%{INFO_SCRIPT_URL}e"
RequestHeader set INFO_SCRIPT_USER "%{INFO_SCRIPT_USER}e"
RequestHeader set INFO_SERVER_ADDR "%{INFO_SERVER_ADDR}e"
RequestHeader set INFO_SERVER_ADMIN "%{INFO_SERVER_ADMIN}e"
RequestHeader set INFO_SERVER_NAME "%{INFO_SERVER_NAME}e"
RequestHeader set INFO_SERVER_PORT "%{INFO_SERVER_PORT}e"
RequestHeader set INFO_SERVER_PROTOCOL "%{INFO_SERVER_PROTOCOL}e"
RequestHeader set INFO_SERVER_SIGNATURE "%{INFO_SERVER_SIGNATURE}e"
RequestHeader set INFO_SERVER_SOFTWARE "%{INFO_SERVER_SOFTWARE}e"
RequestHeader set INFO_THE_REQUEST "%{INFO_THE_REQUEST}e"
RequestHeader set INFO_TIME "%{INFO_TIME}e"
RequestHeader set INFO_TIME_DAY "%{INFO_TIME_DAY}e"
RequestHeader set INFO_TIME_HOUR "%{INFO_TIME_HOUR}e"
RequestHeader set INFO_TIME_MIN "%{INFO_TIME_MIN}e"
RequestHeader set INFO_TIME_MON "%{INFO_TIME_MON}e"
RequestHeader set INFO_TIME_SEC "%{INFO_TIME_SEC}e"
RequestHeader set INFO_TIME_WDAY "%{INFO_TIME_WDAY}e"
RequestHeader set INFO_TIME_YEAR "%{INFO_TIME_YEAR}e"
RequestHeader set INFO_TZ "%{INFO_TZ}e"
RequestHeader set INFO_UNIQUE_ID "%{INFO_UNIQUE_ID}e"
</pre>








<h2><a id="decoded"></a>Mod_Rewrite Variables Decoded!</h2>
<pre>
[API_VERSION] =&gt; 20020903:12
[AUTH_TYPE] =&gt; Digest
[DOCUMENT_ROOT] =&gt; /home/user/www_root/askapache.com
[HTTPS] =&gt; off
[HTTP_ACCEPT] =&gt; text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
[HTTP_COOKIE] =&gt; PHPSESSID=752ee6d56e15f305233e30045987e5ce568c034; __qca=1176541225-59967328-5223185;
[HTTP_HOST] =&gt; www.askapache.com
[HTTP_REFERER] =&gt; http://www.askapache.com/protest/index.php?askapache=awesomeness&amp;you=rock
[HTTP_USER_AGENT] =&gt; Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16
[IS_SUBREQ] =&gt; false
[QUERY_STRING] =&gt; e=404
[REMOTE_ADDR] =&gt; 22.162.144.211
[REMOTE_HOST] =&gt; 22.162.144.211
[REMOTE_PORT] =&gt; 4511
[REMOTE_USER] =&gt; administrator
[REQUEST_FILENAME] =&gt; /home/user/www_root/askapache.com/protest/index.php
[REQUEST_METHOD] =&gt; GET
[REQUEST_URI] =&gt; /protest/index.php
[SCRIPT_FILENAME] =&gt; /home/user/www_root/askapache.com/protest/index.php
[SCRIPT_GROUP] =&gt; daemonu
[SCRIPT_USER] =&gt; askapache
[SERVER_ADDR] =&gt; 208.113.134.190
[SERVER_ADMIN] =&gt; webmaster@askapache.com
[SERVER_NAME] =&gt; www.askapache.com
[SERVER_PORT] =&gt; 80
[SERVER_PROTOCOL] =&gt; HTTP/1.1
[SERVER_SOFTWARE] =&gt; Apache/2.0.61 (Unix) PHP/4.4.7 mod_ssl/2.0.61 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2
[THE_REQUEST] =&gt; GET /protest/adf HTTP/1.1
[TIME] =&gt; 20080820014309
[TIME_DAY] =&gt; 20
[TIME_HOUR] =&gt; 01
[TIME_MIN] =&gt; 43
[TIME_MON] =&gt; 08
[TIME_SEC] =&gt; 09
[TIME_WDAY] =&gt; 3
[TIME_YEAR] =&gt; 2008
</pre>


<h3>Request using HTTPS</h3>
<pre>
[API_VERSION] =&gt; 20020903:12
[AUTH_TYPE] =&gt; Digest
[DOCUMENT_ROOT] =&gt; /home/user/www_root/askapache.com
[HTTPS] =&gt; on
[HTTP_ACCEPT] =&gt; text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
[HTTP_COOKIE] =&gt; PHPSESSID=752ee6d56e15f305233e30045987e5ce568c034; __qca=1176541225-59967328-5223185;
[HTTP_HOST] =&gt; www.askapache.com
[HTTP_REFERER] =&gt; http://www.askapache.com/protest/index.php?askapache=awesomeness&amp;you=rock
[HTTP_USER_AGENT] =&gt; Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16
[IS_SUBREQ] =&gt; false
[QUERY_STRING] =&gt; hi=you&amp;whats=&amp;amp;you
[REMOTE_ADDR] =&gt; 22.162.144.211
[REMOTE_HOST] =&gt; 22.162.144.211
[REMOTE_PORT] =&gt; 4605
[REMOTE_USER] =&gt; administrator
[REQUEST_FILENAME] =&gt; /home/user/www_root/askapache.com/protest/index.php
[REQUEST_METHOD] =&gt; GET
[REQUEST_URI] =&gt; /protest/index.php
[SCRIPT_FILENAME] =&gt; /home/user/www_root/askapache.com/protest/index.php
[SCRIPT_GROUP] =&gt; daemonu
[SCRIPT_USER] =&gt; askapache
[SERVER_ADDR] =&gt; 208.113.134.190
[SERVER_ADMIN] =&gt; webmaster@askapache.com
[SERVER_NAME] =&gt; www.askapache.com
[SERVER_PORT] =&gt; 443
[SERVER_PROTOCOL] =&gt; HTTP/1.1
[SERVER_SOFTWARE] =&gt; Apache/2.0.61 (Unix) PHP/4.4.7 mod_ssl/2.0.61 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2
[THE_REQUEST] =&gt; GET /protest/index.php?hi=you&amp;whats=&amp;amp;you HTTP/1.1
[TIME] =&gt; 20080820015016
[TIME_DAY] =&gt; 20
[TIME_HOUR] =&gt; 01
[TIME_MIN] =&gt; 50
[TIME_MON] =&gt; 08
[TIME_SEC] =&gt; 16
[TIME_WDAY] =&gt; 3
[TIME_YEAR] =&gt; 2008
</pre>





<h2>Emulating ErrorDocuments with Mod_Rewrite</h2>
<p>The ErrorDocument directive is helpful because an errordocument is called differently then a normal file, and it contains special variables to help an admin debug.</p>
<p>I've wanted to use a RewriteCond + a RewriteRule to cause an Apache ErrorDocument to be displayed for a long time... I finally figured it out.  Simply use the <a href="http://www.askapache.com/htaccess/apache-status-code-headers-errordocument.html">HTTP STATUS CODE trick</a> in combination with a simple RewriteRule to trigger an Apache ErrorDocument.</p>

<p>This code emulates the internal 404 process Apache goes through.. If the file is not found it requests the /test/trigger-error/404 internally which triggers the 404 ErrorDocument.</p>
<pre>
ErrorDocument 404 /test/errordocument/404.html
Redirect 404 /test/trigger-error/404
&nbsp;
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} !=404
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /test/trigger-error/404 [L]
</pre>

<p><strong>Big Deal..</strong> you might say... well consider that this works with any status code, and using this method you now have the power to trigger any errordocument page based on any kind of rewritecond.  I'll be writing about some practical uses for this powerful method in the coming weeks, but heres a good example now so you can see how it can be used.</p>

<p>This bit of code Triggers the <a href="http://www.askapache.com/htaccess/apache-status-code-headers-errordocument.html#status-505">505 HTTP Version Not Supported</a> When a request is made to the server with a protocol other than 1.1.</p>
<pre>
ErrorDocument 505 /test/errordocument/505.html
Redirect 505 /test/trigger-error/505
&nbsp;
RewriteEngine On
RewriteBase /
&nbsp;
RewriteCond %{ENV:REDIRECT_STATUS} !=505
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /.*\ HTTP/(0\.9|1\.0|1\.1) [NC]
RewriteRule .* /test/trigger-error/505 [L]
</pre>



<p class="anote"><strong>YES!</strong> I realize I didn't explain that very well, I didn't realize it was that complicated..  I wanted to go into how to use these advanced tricks and methods to achieve some really cool stuff, but explaining just this little bit took me awhile and I'm out of page space!<br /><br />So play around with this and I'll post back some of the <em>untapped sicknesses</em> you can give a website with such powerful methods at your disposal.</p>







<blockquote cite="http://httpd.apache.org/">
<cite><strong>Ralf S. Engelschall</strong></cite>
<pre>
/*
 *  URL Rewriting Module
 *
 *  This module uses a rule-based rewriting engine (based on a
 *  regular-expression parser) to rewrite requested URLs on the fly.
 *
 *  It supports an unlimited number of additional rule conditions (which can
 *  operate on a lot of variables, even on HTTP headers) for granular
 *  matching and even external database lookups (either via plain text
 *  tables, DBM hash files or even external processes) for advanced URL
 *  substitution.
 *
 *  It operates on the full URLs (including the PATH_INFO part) both in
 *  per-server context (httpd.conf) and per-dir context (.htaccess) and even
 *  can generate QUERY_STRING parts on result.   The rewriting result finally
 *  can lead to internal subprocessing, external request redirection or even
 *  to internal proxy throughput.
 *
 *  This module was originally written in April 1996 and
 *  gifted exclusively to the The Apache Software Foundation in July 1997 by
 *
 *      Ralf S. Engelschall
 *      rse engelschall.com
 *      www.engelschall.com
 */
</pre>
</blockquote><p><a href="http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html"></a><a href="http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html">Crazy Advanced Mod_Rewrite Debug Tutorial</a> originally appeared on <cite>AskApache.com</cite> </p>]]></content:encoded>
			<wfw:commentRss>http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html/feed</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>Pimp out your FeedBurner Count</title>
		<link>http://www.askapache.com/hacking/pimp-feedburner-count.html</link>
		<comments>http://www.askapache.com/hacking/pimp-feedburner-count.html#comments</comments>
		<pubDate>Tue, 19 Aug 2008 08:50:18 +0000</pubDate>
		<dc:creator>AskApache</dc:creator>
				<category><![CDATA[Hacking]]></category>

		<guid isPermaLink="false">http://www.askapache.com/?p=1154</guid>
		<description><![CDATA[<p><a class="IFL" id="rss1a" href="http://www.askapache.com/htaccess/pimp-out-your-feedburner-count.html"></a>I've had a lot of people ask about the FeedBurner FeedCount image on AskApache.   Specifically how to set it up with custom messages and different colors each page view... It is pretty sweet..<br class="C" /></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.askapache.com/hacking/pimp-feedburner-count.html"></a><a href="http://www.askapache.com/hacking/pimp-feedburner-count.html"><cite>AskApache.com</cite></a></p><p><a href="http://feeds.askapache.com/apache/htaccess"><img src="http://static.askapache.com/feed.gif" width="176" height="52" alt="wink" title="feed hacking" /></a></p>
<p>Alot of you have contacted me about the <a href="http://www.feedburner.com/">FeedBurner</a> <a href="http://feeds.askapache.com/apache/htaccess">FeedCount image on AskApache</a>.  Most people are curious as to how I can display custom text and colors.  Also there have been alot of questions about how I get it to <em>change colors with every page view</em>.  Here you go!</p>


<h2>Customizing the FeedCount Image</h2>
<p>There are several parameters that can manipulate the feedcount image by adding them onto the feedcount image url.  For example, my image url is <code>http://feeds.askapache.com/~fc/apache/htaccess</code>, so I just add on any of the following...  Play around with them, they aren't documented officially.</p>
<dl>
<dt><a title="feedcount background color" id="feedcount-bg"><dfn title="feedcount background">bg</dfn></a></dt>
<dd>Specify the background color for the feedcount image, like <code>bg=3366CC</code></dd>
<dt><a title="feedcount foreground color" id="feedcount-fg"><dfn title="feedcount foreground">fg</dfn></a></dt>
<dd>The font color for feed count <code>fg=333333</code></dd>
<dt><a title="feedcount animation" id="feedcount-anim"><dfn title="feedcount animation">anim</dfn></a></dt>
<dd>If 0, it won't animate.  If 10, it will animate 10 times.. or 50  <code>anim=50</code></dd>
<dt><a title="feedcount custom label" id="feedcount-label"><dfn title="feedcount custom message">label</dfn></a></dt>
<dd>This lets you specify a custom label built in to the image, instead of "readers".. like <code>label=AxeApache</code></dd>
</dl>


<h2>Loading Different FeedCount Images</h2>
<p>I simply came up with 10 image configurations I liked using the GET parameters above, then I wrote a script to download all 10 of them to my server every 15 minutes.   Next I setup a simple <a href="http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html">mod_rewrite rule in .htaccess</a> that allows me to serve any of the 10 images using 1 url.</p>

<p>So when a user requests my feed image at http://static.askapache.com/feed.gif, they will in fact be seeing 1 of the 10 images, because their is no /feed.gif image.</p>

<h3>Making /feed.gif Never Cache</h3>
<p>In order for the image to be reloaded on each page-view of the site, I set up some anti-caching headers in my .htaccess file.</p>
<p>First I unset the far-future Expires headers that I normally send with images, and then I instruct browsers to check the image every request to see if it has been modified.</p>
<pre>&lt;filesMatch "^feed(1|2|3|4)?\.gif$"&gt;
Header unset Expires
Header unset Last-Modified
FileETag None
Header set Cache-Control "no-cache, must-revalidate"
&lt;/filesMatch&gt;</pre>


<h3>Rewriting /feed.gif to feed1-4.gif</h3>
<p>I save the 4 images in their own folder, so the rewrites only apply to them. This loads a different image file depending on the second of the request.</p>

<p class="anote"><strong>Update!</strong><br /> This code works even better than the one below it but you have to have all 10 images.</p>
<pre>RewriteEngine On
RewriteBase /
&nbsp;
RewriteCond %{TIME_SEC} ^.([0-9]) [NC]
RewriteRule ^feed\.gif$ /feed%1.gif [NC,L]</pre>

<pre>RewriteEngine On
RewriteBase /
RewriteRule !\.gif - [S=4]
&nbsp;
RewriteCond %{TIME_SEC} ^(0|4|8|12|16|22|26|30|34|38|42|46|50|54|58)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^feed\.gif$ /zi/feed1.gif [S=3]
&nbsp;
RewriteCond %{TIME_SEC} ^(1|5|9|13|17|23|27|31|35|39|43|47|51|55|59)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^feed\.gif$ /zi/feed2.gif [S=2]
&nbsp;
RewriteCond %{TIME_SEC} ^(2|6|10|14|18|24|28|32|36|40|44|48|52|56|60)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^feed\.gif$ /zi/feed3.gif [S=1]
&nbsp;
RewriteCond %{TIME_SEC} ^(3|7|11|15|19|25|29|33|37|41|45|49|53|57)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^feed\.gif$ /zi/feed4.gif</pre>



<h2>Bash Shell Script</h2>
<p>This simple shell script is what I use, it downloads 4 different feedcount images with different background/foreground colors and with custom labels.  It saves them to the BASE folder.</p>
<pre class='prebash'>#!/bin/bash
umask 022
&nbsp;
BASE="/home/askapache.com/i/feed"
FEEDURL="http://feeds.askapache.com/~fc/apache/htaccess\?bg="
FEEDOPT="\&amp;fg=FFFFFF\&amp;anim=1\&amp;label="
CURLOPTS="-s -S -L -A &#039;Mozilla/5.0&#039; -m 400"
&nbsp;
curl $CURLOPTS -o ${BASE}1.gif ${FEEDURL}3366CC${FEEDOPT}chilling
curl $CURLOPTS -o ${BASE}2.gif ${FEEDURL}333333${FEEDOPT}smoking
curl $CURLOPTS -o ${BASE}3.gif ${FEEDURL}228800${FEEDOPT}on\%20fire
curl $CURLOPTS -o ${BASE}4.gif ${FEEDURL}BB0000${FEEDOPT}loving
&nbsp;
exit 0</pre>


<h2>Crontab Entry</h2>
<p>This crontab runs the above shell script every 15 minutes, ensuring you have an up-to-date feed count.</p>
<pre class='prebash'>*/15 * * * * /home/cron/feedcount.sh &amp;&gt;/dev/null</pre>





<blockquote cite="http://www.feedburner.com/fb/a/feedcount" title="FeedCount"><h2>FeedCount</h2>
<p>FeedCount is another cool way to promote your feed. It is a dynamic graphic that always displays your feed's current circulation, as calculated by FeedBurner. Choose its color scheme and an animated or static appearance; it is yours to customize!</p></blockquote>
<p><a href="http://feeds.feedburner.com/apache/htaccess">See how this looks.</a></p>
<p><a href="http://uploads.askapache.com/2008/08/feedcount1.png"><img src="http://uploads.askapache.com/2008/08/feedcount1-400x310.png" alt="Pimp out your FeedBurner Count" title="feedcount1" width="400" height="310" class="alignnone size-medium wp-image-1173" /></a></p>



<blockquote cite="http://www.feedburner.com/fb/a/feedimage"><h2>Feed Image Burner</h2>
<p>Add some color to your feed. Place a special image in your feed so that it stands out from the pack when displayed in many popular RSS news readers. This service Adds an image tag block to your feed, which refers to the web location of the image you choose below. Most RSS readers display this image in either individual or grouped views of the news items in a feed. If you provide your own image, it is recommended to be no more than 144 pixels tall or wide.</p>
</blockquote>
<p><a href="http://uploads.askapache.com/2008/08/feedimage.png"><img src="http://uploads.askapache.com/2008/08/feedimage-400x372.png" alt="Pimp out your FeedBurner Count" title="Feed Image Burner" width="400" height="372" class="alignnone size-medium wp-image-1174" /></a></p>




<p><img src="http://feeds.askapache.com/~fc/apache/htaccess?bg=336600&amp;anim=0&amp;label=FFFFFF&amp;fg=000000" alt="Pimp out your FeedBurner Count"  title=" hacking" /> <img src="http://feeds.askapache.com/~fc/apache/htaccess?bg=ff0000&amp;anim=0&amp;label=FFFFCC&amp;fg=000000" alt="Pimp out your FeedBurner Count"  title=" hacking" /> <img src="http://feeds.askapache.com/~fc/apache/htaccess?bg=228800&amp;anim=0&amp;label=FFFF99&amp;fg=000000" alt="Pimp out your FeedBurner Count"  title=" hacking" /> <img src="http://feeds.askapache.com/~fc/apache/htaccess?bg=003300&amp;anim=0&amp;label=FFFF66&amp;fg=000000" alt="Pimp out your FeedBurner Count"  title=" hacking" /> <img src="http://feeds.askapache.com/~fc/apache/htaccess?bg=333&amp;anim=0&amp;label=FFFF33&amp;fg=000000" alt="Pimp out your FeedBurner Count"  title=" hacking" /> <img src="http://feeds.askapache.com/~fc/apache/htaccess?bg=880000&amp;anim=0&amp;label=FFFF00&amp;fg=000000" alt="Pimp out your FeedBurner Count"  title=" hacking" /></p>



<h2>Advanced FeedBurner Stats</h2>
<ul>
	<li><a href="http://api.feedburner.com/awareness/1.0/GetItemData?uri=http://feeds.feedburner.com/apache/htaccess">GetItemData</a></li>
	<li><a href="http://api.feedburner.com/awareness/1.0/GetFeedData?uri=http://feeds.feedburner.com/apache/htaccess">GetFeedData</a></li>
	<li><a href="http://api.feedburner.com/awareness/1.0/GetResyndicationData?uri=http://feeds.feedburner.com/apache/htaccess">GetResyndicationData</a></li>
</ul>


<p>Thats it!  Not very impressive technically, but a cool thing to have on your site.. the changing colors and custom labels work great at grabbing peoples attention.</p><p><a href="http://www.askapache.com/hacking/pimp-feedburner-count.html"></a><a href="http://www.askapache.com/hacking/pimp-feedburner-count.html">Pimp out your FeedBurner Count</a> originally appeared on <cite>AskApache.com</cite> </p>]]></content:encoded>
			<wfw:commentRss>http://www.askapache.com/hacking/pimp-feedburner-count.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

