<?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>philihp.com &#187; mod_rewrite</title>
	<atom:link href="http://www.philihp.com/blog/tag/mod_rewrite/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.philihp.com/blog</link>
	<description>I do things, and then I tell the internet about them.</description>
	<lastBuildDate>Mon, 06 Feb 2012 05:40:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Redirecting with mod_rewrite without breaking OpenID</title>
		<link>http://www.philihp.com/blog/2009/redirecting-with-mod_rewrite-without-breaking-openid/</link>
		<comments>http://www.philihp.com/blog/2009/redirecting-with-mod_rewrite-without-breaking-openid/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 06:27:18 +0000</pubDate>
		<dc:creator>philihp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[OpenID]]></category>
		<category><![CDATA[phpMyID]]></category>

		<guid isPermaLink="false">http://www.philihp.com/blog/?p=378</guid>
		<description><![CDATA[On philihp.com, I redirect the root address philihp.com to philihp.com/blog. I do this with mod_rewrite, since it&#8217;s much faster to do an HTTP Location header based redirect than to use an HTML meta tag redirect. I encountered a problem with this, though. I want to be able to authenticate with the identifier &#8220;philihp.com&#8221;. Not &#8220;openid.philihp.com&#8221;, [...]]]></description>
			<content:encoded><![CDATA[<p>On philihp.com, I redirect the root address philihp.com to philihp.com/blog. I do this with mod_rewrite, since it&#8217;s much faster to do an HTTP Location header based redirect than to use an HTML meta tag redirect. I encountered a problem with this, though.</p>
<p>I want to be able to authenticate with the identifier &#8220;philihp.com&#8221;. Not &#8220;openid.philihp.com&#8221;, not &#8220;philihp.com/openid&#8221;, just simply &#8220;philihp.com&#8221;, and I have it all setup using phpMyID to host locally, which is good. But rather than put all the server machinery for authenticating at my root level, I like to have it at &#8220;philihp.com/openid&#8221; (or elsewhere), and out of the way. OpenID has facility for doing this, since it&#8217;s almost always done; when a browser visits philihp.com, in the <code>HEAD</code> tags of the HTML, there&#8217;s the following:</p>
<pre>&lt;link rel="openid.server" href="<i>http://philihp.com/openid/</i>" /&gt;
&lt;link rel="openid.delegate" href="<i>http://philihp.com/</i>" /&gt;
</pre>
<p>Where the <code>openid.server</code> specifies where the machinery is for taking care of the authentication, and the <code>openid.delegate</code> specifies what the claimed identifier is. What happens when you go to somewhere like <a href="http://twitterfeed.com">twitterfeed.com</a>, when you try to authenticate there the server itself goes to the URL you give it, looks at the <code>HEAD</code> tags of the HTML it sees there, and forwards the end user to the <code>openid.server</code> specified. The problem I had is that when the server goes to philihp.com, it&#8217;s being redirected from mod_rewrite to blog/ because of <a href="/blog/2009/01/redirect-root-path-to-blog-directory/">this thing</a>. This is bad, because it causes my claimed identifier to be &#8220;philihp.com/blog/&#8221; instead of just &#8220;philihp.com&#8221;.</p>
<p>I wasn&#8217;t the first person to have this problem, though. <a href="http://qedx.com/blog/redirecting-without-breaking-openid/2009/01/07/">Haris bin Ali</a> and <a href="http://willnorris.com/2008/12/challenges-in-changing-my-openid">Will Norris</a> found a solution with PHP that checks the HTTP headers and redirects based on them. Below these have been adapted to RewriteCond rules, which would get evaluated before PHP or any other platform your site runs on (so long as it&#8217;s still Apache).</p>
<pre>RewriteCond %{HTTP_USER_AGENT} !openid [NC]
RewriteCond %{HTTP_USER_AGENT} !^$
RewriteCond %{HTTP_ACCEPT} !application/xrds+xml [NC]
RewriteRule ^$ /blog/ [R,L]</pre>
<p>These say &#8220;Redirect empty requests (requests for root) to /blog/, unless the User-Agent header has &#8220;openid&#8221; in it, or is blank, or the Accept header contains &#8220;application/xrds+xml&#8221;"; which are all going to be characteristic of a request coming from an OpenID consumer, and not from a normal browser.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.philihp.com/blog/2009/redirecting-with-mod_rewrite-without-breaking-openid/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>.htaccess mod_rewrite Redirect Root / path to /blog directory</title>
		<link>http://www.philihp.com/blog/2009/redirect-root-path-to-blog-directory/</link>
		<comments>http://www.philihp.com/blog/2009/redirect-root-path-to-blog-directory/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 11:27:32 +0000</pubDate>
		<dc:creator>philihp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[mod_rewrite]]></category>

		<guid isPermaLink="false">http://www.philihp.com/blog/?p=252</guid>
		<description><![CDATA[I&#8217;ve just setup an HTTP Redirect from www.philihp.com to www.philihp.com/blog. This makes it so people don&#8217;t see my ugly project listing when they come here, they go right to the blog. I still wanted people to be able to go to the root path, though, because a lot of times whenever I put some small [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just setup an HTTP Redirect from <a href="http://www.philihp.com/">www.philihp.com</a> to <a href="http://www.philihp.com/blog">www.philihp.com/blog</a>. This makes it so people don&#8217;t see my ugly project listing when they come here, they go right to the blog. I still wanted people to be able to go to the root path, though, because a lot of times whenever I put some small project, gizmo, or widget up, I&#8217;ll just stuff it in a new directory from the root path. I settled on allowing only listing when the HTTP header value HTTP_REFERER begins with a path from my blog.</p>
<p>To replicate this again elsewhere, create an <code>.htaccess</code> file in your root directory and put in it (or it if it exists, append to it) the following:</p>
<pre>RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www.philihp.com/ [NC]
RewriteRule ^$ /blog/ [R,L]</pre>
<p>The first line turns mod_rewrite on. It&#8217;s off by default to make serving static files very fast, but mod_rewrite is very fast, and we need it anyway, so turn it on. RewriteRules come in chunks, where every RewriteRule has the RewriteCond<em>itions</em> that came immediately before applied to it. So the second and third lines come as a chunk, which reads &#8220;If the path is empty (i.e. root path), and the HTTP_REFERER[<em>sic</em>] variable does not (<code>!</code>) begin (<code>^</code>) with <code>http://www.philihp.com/</code>, send the request to <code>/blog/</code>. But wait, I have 2 flags, <code>R</code> to Redirect instead of <a href="http://www.giantgeek.com/blog/?p=109">Forward</a>, and <code>L</code> because this is the Last rule we need to process for this request.</p>
<p>The bad thing about this practice is similar to the problem we used to have where <a href="http://www.456bereastreet.com/archive/200411/who_framed_the_web_frames_and_usability/">HTML frames would break bookmarks</a>. A user might go to the directory listing and bookmark it, but if they return, their HTTP_REFERER is different, and the redirect would trigger. But in my case, that&#8217;s not a terribly terrible to the user.</p>
<p>It&#8217;s not too difficult for a malicious attacker to craft their own HTTP_REFERER tag, so be careful depending on it for anything where security is a priority.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.philihp.com/blog/2009/redirect-root-path-to-blog-directory/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

