<?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>Technology, The Web, and Oxford Commas. &#187; mod_rewrite</title>
	<atom:link href="http://mallinson.ca/tag/mod_rewrite/feed/" rel="self" type="application/rss+xml" />
	<link>http://mallinson.ca</link>
	<description>by Chris Mallinson</description>
	<lastBuildDate>Sat, 21 Apr 2012 06:07:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Apache, mod_rewrite and Sexy URLs</title>
		<link>http://mallinson.ca/post/mod_rewrite/</link>
		<comments>http://mallinson.ca/post/mod_rewrite/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 08:10:40 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Hosting]]></category>
		<category><![CDATA[The Web]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[URL Rewriting]]></category>
		<category><![CDATA[Web Servers]]></category>

		<guid isPermaLink="false">http://mallinson.ca/?p=147</guid>
		<description><![CDATA[99% of the people who visit a web site don&#8217;t care how it works, or how it was built. They don&#8217;t care if you&#8217;re using ColdFusion or PHP, and they don&#8217;t care if you&#8217;re using post or get variables. The remaining one percent comprises some techie types like me, and probably you, and some people with less than honourable intentions. [...]]]></description>
			<content:encoded><![CDATA[<p>99% of the people who visit a web site don&#8217;t care how it works, or how it was built. They don&#8217;t care if you&#8217;re using ColdFusion or PHP, and they don&#8217;t care if you&#8217;re using post or get variables. The remaining one percent comprises some techie types like me, and probably you, and some people with less than honourable intentions. For members of the latter group, you don&#8217;t WANT them to know what type of technology you use.</p>
<p>This is a good argument for URL rewriting, as is the fact that semantic and descriptive URLs assist in search engine ranking, and general usability. I rewriting my URLs because I like the way they look. I think a website looks much more polished when the URLs are descriptive, free of variable/value pairs, and as short possible.<br />
<span id="more-147"></span><br />
Since I tend to use Apache with most of the sites I work on, and I recommend you do the same wherever possible, I&#8217;m only going to talk about using mod_rewrite, an almost universal Apache module for URL rewriting. There are similar solutions available for other web servers. I&#8217;ll show you an example use of mod_rewrite for a skeleton example site. I&#8217;ll provide all the code as well so you can play around with a working example. Please remember that this is just a quick overview of the mod_rewrite functions, and how they can be used with ColdFusion. It is always a good idea to lock down your Apache install as much  as possible for public sites, and it should also be noted that using mod_rewrite does involve a small performance hit.</p>
<h3>Turn on mod_rewrite</h3>
<p>You may already have the mod_rewrite module activated in Apache. It&#8217;s simple to check. You need to be familiar with editing the httpd.conf file for your Apache install.</p>
<p>Make sure the following line in your <b>httpd.conf</b> file is NOT commented out (it commented out by default).</p>
<pre lang="” htaccess”">LoadModule rewrite_module libexec/apache2/mod_rewrite.so</pre>
<p>Another thing you need to be sure of is that the AllowOverride directive is turned on for your web root.  The default settings in apache is &#8220;none&#8221;.  This needs to be changed to &#8220;all&#8221; to enable the .htaccess file to change Apache settings.  This can be found either in the default  section it can be set on a per directory basis.</p>

<div class="wp_codebox"><table><tr id="p1473"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p147code3"><pre class="htaccess" style="font-family:monospace;">&lt;directory /&gt;
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Deny from all
&lt;/directory&gt;</pre></td></tr></table></div>

<p>If you need to make a change, remember to stop and restart Apache for your changes to take effect.</p>
<h3>Looking at the Rewrite Directives</h3>
<p>Here&#8217;s my example .htaccess file. This file goes in your web root. I&#8217;ll go through it step by step in a minute.  One important prerequisite for understanding the mod_rewrite file is that you will need to have a good understanding of regular expressions in order to write your own rules.</p>

<div class="wp_codebox"><table><tr id="p1474"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p147code4"><pre class="htaccess" style="font-family:monospace;">RewriteEngine on
&nbsp;
RewriteRule ^$ app/index.cfm [L]
RewriteRule ^(contact|about|blog|categories)/?$ app/$1.cfm [L]
RewriteRule ^demos/([0-9]*)/?$ app/content/demo$1/index.cfm [L]
RewriteRule ^c/([a-zA-Z0-9\-\_]{1,30})/?$ app/categories.cfm?cat=$1 [L]
RewriteRule ^([a-zA-Z0-9\-\_]{1,30})/?$ app/post.cfm?key=$1 [L]
&nbsp;
RewriteCond %{HTTP_USER_AGENT} MSIE\s(\d)\.(?!.*Opera)
RewriteCond %1 &amp;lt;=6
RewriteRule ^(/[a-zA-Z0-9\-\_\.])*assets/css/main\.css$ css/main-ie.css [L]
&nbsp;
RewriteRule ^(/[a-zA-Z0-9\-\_\.])*assets/js/jquery\.js$ js/jquery/jquery-1.3.2.min.js [L]
RewriteRule ^(/[a-zA-Z0-9\-\_\.])*assets/css/main\.css$ css/main.css [L]</pre></td></tr></table></div>

<p><b title="RewriteEngine on">Line 1</b> is important, as it activates the Rewrite engine.</p>
<p>The rewriting starts on <b title="RewriteRule ^$ app/index.cfm [L]">line 3</b>. I like to keep my main site files in a folder called &#8220;app&#8221; in my web root, so the first line rewrites a call to the web root to &#8220;/app/index.cfm&#8221;</p>
<p><b title="RewriteRule ^(contact|about|blog|categories)/?$ app/$1.cfm [L]">Line 4</b> redirects a few of the regular top-level pages in the site to their respective locations in my directory structure. /contact to /app/contact.cfm, /about to app/about.cfm, etc.</p>
<p><b title="RewriteRule ^demos/([0-9]*)/?$ app/content/demo$1/index.cfm [L]">Line 5</b> redirects links to demos I included on the site.  /demo/1 goes to /app/demo1/index.cfm, /demo/2 goes to /app/demo2/index.cfm, and so on.</p>
<p><b title="RewriteRule ^c/([a-zA-Z0-9\-\_]{1,30})/?$ app/categories.cfm?cat=$1 [L]">Line 6</b> is for the categories pages for the blog that is part of the site.  For example, the link /c/cars would get redirected to /app/categories.cfm?cat=cars.</p>
<p><b title="RewriteRule ^([a-zA-Z0-9\-\_]{1,30})/?$ app/post.cfm?key=$1 [L]">Line 7</b> will take any remaining link that does not contain a &#8220;sub-directory&#8221; and assumes it is a blog post.  The links would be forwarded like this:  /apache-url-forwarding would be rewritten as /app/post.cfm?key=apache-url-forwarding. This allows you to use the key to lookup a blog post and display it for the reader.  These short titles are usually called &#8220;slugs&#8221; by blogging software and are unique titles given to your posts for use in search engine friendly URLs.</p>
<p><b title="RewriteCond %{HTTP_USER_AGENT} MSIE\s(\d)\.(?!.*Opera)  -  RewriteCond %1 &lt;=6">Line 9 and 10</b> are conditional, and serve the purpose of rewriting the CSS file differently for Internet Explorer 6 and below than it is for the rest of the browsers.  You can see that <b title="RewriteRule ^(/[a-zA-Z0-9\-\_\.])*assets/css/main\.css$ css/main-ie.css [L]">line 11</b> directs calls to the main CSS file to an IE6 specific file.  The preceding rewrite conditions apply to the rewrite rule that immediately follows the condition.</p>
<p><b title="RewriteRule ^(/[a-zA-Z0-9\-\_\.])*assets/js/jquery\.js$ js/jquery/jquery-1.3.2.min.js [L]">Line 13</b> rewrites the CSS file for the remaining browsers.  You may wonder why I bother rewriting the URL of the CSS file.  I like to keep several sets of CSS files on hand &#8211; sometimes different themes, sometimes different versions, and sometimes just a specific set of CSS files for development only.  Rewriting the CSS URLs allows me to switch between the files with ease without touching my application code at all.</p>
<p><b title="RewriteRule ^(/[a-zA-Z0-9\-\_\.])*assets/css/main\.css$ css/main.css [L]">Line 14</b> rewrites the call to an external JavaScript file &#8211; in this case jQuery.  This is helpful for managing versions of a jQuery library.  In this case, I&#8217;m just calling a file called jquery.js, but I&#8217;m linking to a minified version of jquery 1.3.2.</p>
<h3>The Code</h3>
<p>I&#8217;ve gone through this pretty quickly, and without a ton of detail, but I&#8217;ve included the code all zipped up nicely for you to try it yourself. Stick the whole directory structure in your development web root, and give it a try. Remember to turn on mod_rewrite.</p>
<p><a href="http://cf.mallinson.ca/url_rewrite/">Working Example</a><br />
<a href="http://cf.mallinson.ca/url_rewrite.zip">Zipped Up Files</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mallinson.ca/post/mod_rewrite/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Object Caching 380/382 objects using disk: basic
Content Delivery Network via Amazon Web Services: S3: assets.mallinson.ca

Served from: mallinson.ca @ 2012-05-18 19:19:23 -->
