<?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>AkillesBlog &#187; caching</title>
	<atom:link href="http://blog.akilles.org/tag/caching/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.akilles.org</link>
	<description>Talk on programming, computers, electronics, web etc</description>
	<lastBuildDate>Sun, 23 Jan 2011 23:46:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Caching of PHP scripts with query strings</title>
		<link>http://blog.akilles.org/2009/01/10/caching-of-php-scripts-with-query-strings/</link>
		<comments>http://blog.akilles.org/2009/01/10/caching-of-php-scripts-with-query-strings/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 19:41:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[url rewrite]]></category>

		<guid isPermaLink="false">http://blog.akilles.org/?p=66</guid>
		<description><![CDATA[Trying to decrease bandwith usage and page loading time, I&#8217;ve enabled caching on some of my web pages recently. Caching can be done in many ways, and with PHP-scripts running on Apache, I have the choice of enabling caching in either .htaccess-files, in the HTML-header or in the HTTP header. I think sending HTTP headers [...]]]></description>
			<content:encoded><![CDATA[<p>Trying to decrease bandwith usage and page loading time, I&#8217;ve enabled caching on some of my web pages recently. Caching can be done in many ways, and with PHP-scripts running on Apache, I have the choice of enabling caching in either .htaccess-files, in the HTML-header or in the HTTP header. I think sending HTTP headers from PHP gives the best control, having all programatically possibilities for conditional caching and headers.</p>
<p>For basic caching of a generated PHP-page I find the &#8220;Expires&#8221; / &#8220;max-age&#8221; HTTP headers the most convenient to use;</p>
<pre class="brush: php; title: ;">
//Cache for 15 minutes:
$maxage = 60*15;
header (&quot;Cache-Control: max-age=$maxage&quot;);
header ('Expires: ' . gmstrftime(&quot;%a, %d %b %Y %H:%M:%S GMT&quot;, time() + $maxage));
</pre>
<p>With this method, the page will be loaded from the user&#8217;s browser-cache instead of from my server until $maxage seconds have passed. After that, a new visit to the page will be freshly loaded from my server to the browser&#8217;s cache again, and will live there for another $maxage seconds. This <strong>works well with</strong> URLs such as </p>
<ul>
<li>http://www.example.com/index.php</li>
<li>http://www.example.com/index.html</li>
<li>http://www.example.com/</li>
<li>http://www.example.com/pages/frontpage/</li>
</ul>
<p><strong>but not with</strong> URLs containing query strings:</p>
<p>http://www.example.com/index.php<em>?page=frontpage&#038;itemcount=10</em></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-0426298800349415";
google_ad_slot = "0902617621";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<h2>The problem with query strings</h2>
<p>If the URL to the page in question contains a query string (starting with the question mark in this URL: http://www.example.com/index.php?page=frontpage&#038;itemcount=10), the HTTP standard says that the browser never should cache that page. Using standard-compliant browsers like Opera, you&#8217;ll have to <strong>get rid of that query string</strong> to enable caching of such pages.</p>
<p>I long scratched my head over this, but was after a while enlightened by <a href="http://www.thinkvitamin.com/features/webapps/serving-javascript-fast">Cal Henderson&#8217;s post</a>:</p>
<blockquote><p>
According the letter of the HTTP caching specification, user agents should never cache URLs with query strings. While Internet Explorer and Firefox ignore this, Opera and Safari don’t &#8211; to make sure all user agents can cache your resources, we need to keep query strings out of their URLs.
</p></blockquote>
<h2>Getting rid of the query string &#8211; URL rewriting</h2>
<p><script type="text/javascript"><!--
google_ad_client = "pub-0426298800349415";
google_ad_slot = "0902617621";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>URL rewriting can be used to <em>tricking</em> the browser into believing we&#8217;re browsing an ordinary page without query strings. URL rewriting means that the user and the user&#8217;s browser enters and sees an URL like http://www.example.com/frontpage.asdf, while the Apache web server internally translates this to, let&#8217;s say, http://www.example.com/index.php?page=frontpage, running your index.php script with the right GET parameters.</p>
<p>Using Apache, this is as easy as writing some RewriteRules in a .htaccess-file, for instance:<br />
<code><br />
RewriteEngine On</p>
<p>#do not rewrite requests for /index.php:<br />
RewriteRule	^index.php$	-			[L]</p>
<p>#rewrite /frontpage.asdf, /photos.asdf<br />
#to /index.php?page=frontpage etc:<br />
RewriteRule	^(.*?).asdf$	/index.php?page=$1	[QSA,L]<br />
</code></p>
<p>More tips and tricks about URL rewriting can be read <a href="http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html">here</a> and many other places. Google it!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.akilles.org/2009/01/10/caching-of-php-scripts-with-query-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

