<?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>Be the signal &#187; rewritecond</title>
	<atom:link href="http://bethesignal.org/blog/tag/rewritecond/feed/" rel="self" type="application/rss+xml" />
	<link>http://bethesignal.org</link>
	<description>where we&#039;re going, we don&#039;t need roads...</description>
	<lastBuildDate>Thu, 26 Jan 2012 05:52:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<atom:link rel='hub' href='http://bethesignal.org/?pushpress=hub'/>
		<item>
		<title>Case-insensitive mapping with mod_rewrite&#8217;s RewriteMap</title>
		<link>http://bethesignal.org/blog/2009/03/29/case-insensitive-mapping-with-mod_rewrites-rewritemap/</link>
		<comments>http://bethesignal.org/blog/2009/03/29/case-insensitive-mapping-with-mod_rewrites-rewritemap/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 16:40:37 +0000</pubDate>
		<dc:creator>Jeff Waugh</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[case-insensitive]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[rewritecond]]></category>
		<category><![CDATA[rewritemap]]></category>
		<category><![CDATA[rewriterule]]></category>

		<guid isPermaLink="false">http://bethesignal.org/?p=1425</guid>
		<description><![CDATA[Sometimes when you need to manage a massive pile of URL redirections &#8212; for instance, when you&#8217;re playing snatch-the-tablecloth with your web platform &#8212; it&#8217;s handy to mash them all together with mod_rewrite&#8217;s RewriteMap feature. I hit a frustrating stumbling &#8230; <a href="http://bethesignal.org/blog/2009/03/29/case-insensitive-mapping-with-mod_rewrites-rewritemap/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sometimes when you need to manage a massive pile of URL redirections &#8212; for instance, when you&#8217;re playing snatch-the-tablecloth with your web platform &#8212; it&#8217;s handy to mash them all together with mod_rewrite&#8217;s RewriteMap feature.</p>
<p>I hit a frustrating stumbling block with a recent project, however: What happens if you need your RewriteMap key to be case-insensitive? This is incredibly likely if you&#8217;re migrating away from an undisciplined Windows-based web platform.</p>
<p>It was immediately obvious that I&#8217;d have to match against consistent casing, but not at all obvious how this would work in the context of mod_rewrite. After some trial and error, here&#8217;s what I came up with:</p>
<pre>RewriteMap lowercase int:tolower
RewriteMap urls dbm:/srv/www.example.com/urls.db
RewriteCond %{REQUEST_URI} ^(/.*\.html)$
RewriteCond ${lowercase:%1} ^(/.*\.html)$
RewriteCond ${urls:%1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(/.*\.html)$ http://%{HTTP_HOST}${urls:%1}? [R=permanent,L]</pre>
<p>Here&#8217;s how it works:</p>
<ol>
<li>First we set up the built in lowercase translation rewrite map&#8230; handy!</li>
<li>Then we define our great big URL map, which is just a list of lowercase URLs (yes, they&#8217;re lowercased in the file) and their destinations.</li>
<li>Make sure we&#8217;re only mucking around with html URLs, but also grab the REQUEST_URI for use further along (see the parentheses in the second parameter).</li>
<li>We use Apache&#8217;s handy internal lowercase map to convert the previous line&#8217;s match to lowercase, and we grab the whole thing again&#8230; but this time, what we&#8217;re grabbing is the output of the lowercase map.</li>
<li>Tricky little hack to make sure our URL is represented in the redirection map. If there&#8217;s no match, the default value returned is NOT_FOUND&#8230; but we only continue to the next line if the map does <em>not</em> return NOT_FOUND.</li>
<li>Finally, permanently redirect to the destination URL, as matched against the lowercase original.</li>
</ol>
<p>Rock and roll.</p>
]]></content:encoded>
			<wfw:commentRss>http://bethesignal.org/blog/2009/03/29/case-insensitive-mapping-with-mod_rewrites-rewritemap/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

