<?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>Adam Becker &#187; seo</title>
	<atom:link href="http://adambecker.info/blog/tag/seo/feed" rel="self" type="application/rss+xml" />
	<link>http://adambecker.info</link>
	<description>A little bit of shake n' bake</description>
	<lastBuildDate>Fri, 27 Aug 2010 04:50:50 +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>How to serve files with mod rewrite</title>
		<link>http://adambecker.info/blog/development/how-to-serve-files-with-mod-rewrite</link>
		<comments>http://adambecker.info/blog/development/how-to-serve-files-with-mod-rewrite#comments</comments>
		<pubDate>Wed, 09 Dec 2009 18:23:03 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[mod rewrite]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[seo]]></category>

		<guid isPermaLink="false">http://adambecker.info/?p=23</guid>
		<description><![CDATA[Have you ever wondered how some websites seem to serve files in a way that looks like folders instead of actual file names? For instance this website. To see this blog post you are linked to the URL http://adambecker.info/development/how-to-serve-files-with-mod-rewrite/ but in actuality the web server is running a PHP file and serving it as if]]></description>
			<content:encoded><![CDATA[<p>Have you ever wondered how some websites seem to serve files in a way that looks like folders instead of actual file names? For instance this website. To see this blog post you are linked to the URL <span id="sample-permalink">http://adambecker.info/development/</span>how-to-serve-files-with-mod-rewrite/ but in actuality the web server is running a PHP file and serving it as if it is being run from the location I just pasted.</p>
<p>Why do this? There are some good reasons for doing this:</p>
<ul>
<li>You can make your URLs friendlier to the people who are visiting your website</li>
<li>Having friendlier URLs means easier to understand URLs for search engines, which is good for search engine optimization</li>
<li>Better security. With the way I&#8217;ve setup this model all of the server files are outside of the browse-able web directory, which means they can only be accessed through the index.php file.</li>
</ul>
<p>Besides your normal code, you basically need two additional files: an <strong>.htaccess</strong> file and a redirection file which I&#8217;ve named <strong>index.php</strong>.</p>
<p>The .htaccess file lives in your root directory and should look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>IfModule mod_rewrite.<span style="color: #202020;">c</span><span style="color: #339933;">&gt;</span>
RewriteEngine On
RewriteBase <span style="color: #339933;">/</span>
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>f
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>d
RewriteRule . <span style="color: #339933;">/</span>index.<span style="color: #202020;">php</span> <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span>
<span style="color: #339933;">&lt;/</span>IfModule<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>If you aren&#8217;t familiar with htaccess files, Google it and you will find some great information about them.</p>
<p>The index.php file also lives in the root directory and should look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'/index.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// due to mod_rewrite every path comes to this file</span>
	<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REDIRECT_URL'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$pages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// we're going to remove all of the empty arguments</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$arg</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arg</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$pages</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$arg</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// if we don't get any arguments we want to give it our main page</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pages</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$pages</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'main'</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// in this model the area's main page is named after the area</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pages</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$pages</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pages</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// in this model we can accept as many nested areas</span>
	<span style="color: #000088;">$includePath</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'../lib/pages/'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pages</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #b1b100;">include</span> <span style="color: #000088;">$includePath</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'../lib/pages/error/404.php'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'There was a serious error, and we\'re sorry for the inconvenence. Please try again soon.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<div id="attachment_35" class="wp-caption alignleft" style="width: 272px"><img class="size-full wp-image-35" title="Sample File Structure" src="http://adambecker.info/wp-content/uploads/2009/12/files.png" alt="Sample File Structure" width="262" height="292" /><p class="wp-caption-text">Sample File Structure</p></div>
<p>The file is pretty self explanatory, but it is important to know that this isn&#8217;t the only way to do it. I&#8217;ve added some functionality for error 404 pages, and an even bigger fatal error. Last, I wanted to include a picture of a sample file structure that this particular example works for. Keep in mind that the <strong>htdocs</strong> directory is the only publicly browsable folder via a web browser in this sample.</p>
]]></content:encoded>
			<wfw:commentRss>http://adambecker.info/blog/development/how-to-serve-files-with-mod-rewrite/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
