<?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>Simon Whatley &#187; .htaccess</title>
	<atom:link href="http://www.simonwhatley.co.uk/tag/htaccess/feed" rel="self" type="application/rss+xml" />
	<link>http://www.simonwhatley.co.uk</link>
	<description>The opposite of every great idea is another great idea</description>
	<lastBuildDate>Wed, 02 Nov 2011 09:28:34 +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>Apache RewriteRule and query strings</title>
		<link>http://www.simonwhatley.co.uk/apache-rewriterule-and-query-strings</link>
		<comments>http://www.simonwhatley.co.uk/apache-rewriterule-and-query-strings#comments</comments>
		<pubDate>Fri, 18 Feb 2011 10:56:20 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Apache HTTP Server]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[SES]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[URL rewriting]]></category>
		<category><![CDATA[webserver]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=3855</guid>
		<description><![CDATA[At first glance, the way the Apache mod_rewrite module handles query strings can be a little intimidating. mod_rewrite works by sitting on your server in a file called htaccess, and “catching” requests for URL‘s. It then checks these URL request against a series of rules and conditions you have set. If the request meets any of the rules and conditions, it applies then necessary changes to the URL, then reprocesses the request with the changes you have directed.]]></description>
			<content:encoded><![CDATA[<p>At first glance, the way the Apache <code>mod_rewrite</code> module handles query strings can be a little intimidating. <code>mod_rewrite</code> works by sitting on your server in a file called <code>htaccess</code>, and &#8220;catching&#8221; requests for <abbr title="Universal Resource Locator">URL</abbr>&#8216;s. It then checks these <abbr title="Universal Resource Locator">URL</abbr> request against a series of rules and conditions you have set. If the request meets any of the rules and conditions, it applies then necessary changes to the <abbr title="Universal Resource Locator">URL</abbr>, then reprocesses the request with the changes you have directed. Apache helpfully provides some <a href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond" title="Apache RewriteCond Directive" target="_blank" rel="nofollow">RewriteCond documentation</a></p>
<p>The most common mistake people make when thinking of <abbr title="Universal Resource Locator">URL</abbr> redirection with <code>mod_rewrite</code>, is they believe it creates something, or changes something. It doesn&#8217;t.</p>
<p>Here is a simple example, redirecting a page dependent upon its query string. The rewrite condition and rule looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^page\.php$ http://www.example.com/page/%1.php [R=302,L]</pre></div></div>

<p>The rewrite condition matches a numerical ID between 0 and 9. According to the official documentation, you would expect the following behaviour:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/page.php?id=1 -&gt; http://www.example.com/page/1.php
/page.php?id=10 -&gt; http://www.example.com/page/10.php</pre></div></div>

<p>However, if you don’t append something new, then <strong>the original query is passed through</strong> by default. This results in the following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/page.php?id=1 -&gt; http://www.example.com/page/1.php?id=1
/page.php?id=10 -&gt; http://www.example.com/page/10.php?id=10</pre></div></div>

<p>If you want to discard the original query string you must append an empty question mark at the end of the rule; the <strong>query string not append</strong> or <strong>query string discard</strong> flag.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^page\.php$ http://www.example.com/page/%1.php? [R=302,L]</pre></div></div>

<p>Putting it all together, here&#8217;s a quick reference for dealing with query string in a RewriteRule.</p>
<p>Keep original query (i.e., the default behaviour)</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RewriteRule ^page\.php$ /target.php [L]
# from http://www.example.com/page.php?foo=bar
# to http://www.example.com/target.php?foo=bar</pre></div></div>

<p>Discard original query</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RewriteRule ^page\.php$ /target.php? [L]
# from http://www.example.com/page.php?foo=bar
# to http://www.example.com/target.php</pre></div></div>

<p>Replace original query</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RewriteRule ^page\.php$ /target.php?bar=baz [L]
# from http://www.example.com/page.php?foo=bar
# to http://www.example.com/target.php?bar=foo</pre></div></div>

<p>Append new query to original query</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RewriteRule ^page\.php$ /target.php?bar=baz [QSA,L]
# from http://www.example.com/page.php?foo=bar
# to http://www.example.com/target.php?foo=bar&amp;bar=foo</pre></div></div>

<p>Dave Child has created a great <a href="http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/" title="mod_rewrite cheat sheet" target="_blank" rel="nofollow">mod_rewrite cheat sheet</a>; a one-page reference sheet, listing flags for the <code>RewriteRule</code> and <code>RewriteCond</code> directives, list of server variables, a regular expression guide and several examples of common rules.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/apache-rewriterule-and-query-strings/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Apache .htaccess query string redirects</title>
		<link>http://www.simonwhatley.co.uk/apache-htaccess-query-string-redirects</link>
		<comments>http://www.simonwhatley.co.uk/apache-htaccess-query-string-redirects#comments</comments>
		<pubDate>Thu, 17 Feb 2011 21:53:42 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Apache HTTP Server]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[SES]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[URL rewriting]]></category>
		<category><![CDATA[webserver]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=3857</guid>
		<description><![CDATA[One of the most common tasks performed by Apache and htaccess is the manipulation of a URL and configuring a redirect for a specific page.]]></description>
			<content:encoded><![CDATA[<p>One of the most common tasks performed by Apache and <code>htaccess</code> is the manipulation of a <abbr title="Universal Resource Locator">URL</abbr> and configuring a redirect for a specific page. Creating a <strong>single page redirect</strong> in Apache is a simple task, which uses <code>mod_alias</code> module.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Redirect /page.php http://www.example.com/target.php</pre></div></div>

<p>More commonly, however, you&#8217;re likely to want to do a <strong>mass-redirection of pages</strong>. To accomplish this, you may use the <code>RedirectMatch</code> directive.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RedirectMatch ^/category/(.*)$ http://www.example.com/topic/$1</pre></div></div>

<p>This will redirect any page from the <code>category</code> folder to the corresponding one in <code>topic</code> folder with a convenient <strong>one-by-one redirect</strong>.</p>
<p>However, neither <code>Redirect</code> nor <code>RedirectMatch</code> allow you to specify a query string for the redirect source. In other words, the following statements are invalid and they&#8217;ll simply be ignored.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;"># single page redirect
Redirect /page.php?id=1  http://www.example.com/page/1
Redirect /page.php?id=10  http://www.example.com/page/10
&nbsp;
# multi-page redirect
RedirectMatch ^/page.php?id=([0-9]*)$  http://www.example.com/page/$1</pre></div></div>

<p>The solution requires a change of focus from Apache&#8217;s <code>mod_alias</code> module to the <code>mod_rewrite</code> module. Here’s an example.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/page\.php$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ http://www.example.com/page/%1.php [L,R=301]</pre></div></div>

<p>The <code>mod_rewrite</code> module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested <abbr title="Universal Resource Locator">URL</abbr>s on the fly. It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule, to provide a really flexible and powerful <abbr title="Universal Resource Locator">URL</abbr> manipulation mechanism. The <abbr title="Universal Resource Locator">URL</abbr> manipulations can depend on various tests, of server variables, environment variables, <abbr title="HyperText Transfer Protocol">HTTP</abbr> headers, or time stamps.</p>
<p>So what does this all mean with respect to the above example?</p>
<p>The first line enables the <code>RewriteEngine</code> module. Note that <code>mod_rewrite</code> Apache module must be installed and enabled in order to use the <code>RewriteEngine</code>.</p>
<p>The <code>RewriteCond</code> statements set all the rewrite conditions. The fourth line, the real rewrite directive, will be executed <strong>if and only if all conditions are satisfied by the current request</strong>.</p>
<p>The first condition is for the page I need to redirect. This condition is included to prevent any unexpected errors if other pages are using the ID variable. Next, I base the rewrite rule on the value for the current request&#8217;s query string. The ID value within the regular expression is &#8220;wrapped&#8221; to be able to reuse the match later as a back-reference.</p>
<p>The final line is the rewrite rule. This line looks similar to the <code>RedirectMatch</code> statement. It specifies the redirection source, then the redirection target. The value captured by the second <code>RewriteCond</code> is referenced in the target with the <code>%N</code> keyword (in this example %1). The <code>RewriteRule</code> also includes a comma-separated list of flags that should be applied to the rule. In this case, <code>L</code> stops the rewriting process immediately whilst <code>R=301</code> specifies a permanent external redirect (301 is an <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html" title="HTTP Status Codes" target="_blank" rel="nofollow">HTTP Status Code</a>).</p>
<p><strong>Further reading:</strong></p>
<ul>
<li><a href="http://httpd.apache.org/docs/2.2/rewrite/" title="Apache URL rewriting guide" target="_blank" rel="nofollow">Apache URL rewriting guide</a></li>
<li><a href="http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html" title="Apache mod_rewrite" target="_blank" rel="nofollow">Apache mod_rewrite</a></li>
<li><a href="http://httpd.apache.org/docs/2.2/mod/mod_alias.html" title="Apache mod_alias" target="_blank" rel="nofollow">Apache mod_alias</a></li>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/apache-htaccess-query-string-redirects/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Configure Apache to GZip Your Components</title>
		<link>http://www.simonwhatley.co.uk/how-to-configure-apache-to-gzip-your-components</link>
		<comments>http://www.simonwhatley.co.uk/how-to-configure-apache-to-gzip-your-components#comments</comments>
		<pubDate>Wed, 03 Feb 2010 15:26:38 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Apache HTTP Server]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[DEFLATE]]></category>
		<category><![CDATA[gzip]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[Yahoo]]></category>
		<category><![CDATA[ZIP]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=2332</guid>
		<description><![CDATA[Compressing your Web components will help speed up your Website. The majority of your visitors will benefit as most all Web browsers support GZip compression. You’ll want to compress all text, which includes HTML, CSS, JavaScript, XML, JSON, etc.]]></description>
			<content:encoded><![CDATA[<p>Compressing your Web components will help speed up your Website.  The majority of your visitors will benefit as most all Web browsers support <abbr title="GNU zip">GZip</abbr> compression.  You’ll want to compress all text, which includes <abbr title="HyperText Markup Language">HTML</abbr>, <abbr title="Cascading Style Sheets">CSS</abbr>, JavaScript, <abbr title="eXtensible Markup Language">XML</abbr>, <abbr title="JavaScript Object Notation">JSON</abbr>, etc.</p>
<p>Apache 2.x uses <code>mod_deflate</code>. Much like <a href="/how-to-set-an-expires-header-in-apache" title="setting expires headers">setting expires headers</a>, this will save you bandwidth and server load, because it allows output from your server to be compressed before being sent to the client over the network.</p>
<p>The deflate module is not compiled by default and must be enabled in the Apache <em>httpd.conf</em> file. Make sure the following is present and uncommented (remove preceding the #):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">LoadModule deflate_module modules/mod_deflate.so</pre></div></div>

<p>To set <abbr title="GNU zip">GZip</abbr> compression, simply add the following to the <virtualHost> section of your Apache <em>vhost</em> configuration:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html</pre></div></div>

<p>Alternatively you can add it to your <em>htaccess</em> file in an <code>&lt;ifModule mod_deflate.c&gt;&lt;/ifModule&gt;</code> block.</p>
<p>All you really need is the first line. The <code>BrowserMatch </code>lines are there to handle issues with older browsers such as Internet Explorer 5.</p>
<p>You can read all about <abbr title="GNU zip">GZip</abbr> by reading Yahoo!’s <a href="http://developer.yahoo.com/performance/rules.html#gzip" title="Yahoo! Best Practices for Speeding Up Your Web Site Guide" target="_blank" rel="nofollow">Best Practices for Speeding Up Your Web Site</a> guide.</p>
<p>Alternatively, read the <a href="http://httpd.apache.org/docs/2.0/mod/mod_deflate.html" title="Apache mod_deflate documentation" target="_blank" rel="nofollow">Apache mod_deflate documentation</a>.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/how-to-configure-apache-to-gzip-your-components/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Set an Expires Header in Apache</title>
		<link>http://www.simonwhatley.co.uk/how-to-set-an-expires-header-in-apache</link>
		<comments>http://www.simonwhatley.co.uk/how-to-set-an-expires-header-in-apache#comments</comments>
		<pubDate>Wed, 03 Feb 2010 15:06:25 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Apache HTTP Server]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[expires header]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[stylesheets]]></category>
		<category><![CDATA[Web browsers]]></category>
		<category><![CDATA[Yahoo! Inc.]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=2323</guid>
		<description><![CDATA[Setting an Expires (or Cache-Control) header in Apache will help speed up your website. I'm running Apache 2.x, and define an expires header for all of the site's static assets (images, stylesheets, and scripts).]]></description>
			<content:encoded><![CDATA[<p>Setting an Expires (or Cache-Control) header in Apache will help speed up your website. I&#8217;m running Apache 2.x, and define an expires header for all of the site&#8217;s static assets (images, stylesheets, and scripts).</p>
<p>In Apache, <code>mod_expires</code> is a module that allows you to set a given period of time to live for web pages and other objects served from web pages. The idea is to inform web browsers how often they should reload objects from the server. This will save you bandwidth and server load, because clients who follow the header will reload objects less frequently.</p>
<p>The expires module is <strong>not</strong> compiled by default and must be enabled in the Apache <em>httpd.conf</em> file. Make sure the following is present and uncommented (remove preceding the #):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">LoadModule expires_module modules/mod_expires.so</pre></div></div>

<p>To set an expires header, simply add the following to the <code>&lt;virtualHost&gt;</code> section of your Apache <em>vhost</em> configuration:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ExpiresActive On
ExpiresByType image/gif &quot;access plus 1 months&quot;
ExpiresByType image/jpg &quot;access plus 1 months&quot;
ExpiresByType image/jpeg &quot;access plus 1 months&quot;
ExpiresByType image/png &quot;access plus 1 months&quot;
ExpiresByType image/vnd.microsoft.icon &quot;access plus 1 months&quot;
ExpiresByType image/x-icon &quot;access plus 1 months&quot;
ExpiresByType image/ico &quot;access plus 1 months&quot;
ExpiresByType application/javascript “now plus 1 months”
ExpiresByType application/x-javascript “now plus 1 months”
ExpiresByType text/javascript “now plus 1 months”
ExpiresByType text/css “now plus 1 months”
ExpiresDefault &quot;access plus 1 days&quot;</pre></div></div>

<p>Alternatively you can add it to your <em>htaccess</em> file in an <code>&lt;ifModule mod_expires.c&gt;&lt;/ifModule&gt;</code> block.</p>
<p>If you need to change the length of time a type expires, simply change <em>access plus 1 months</em> to the appropriate length of time, e.g. <em>access plus 1 days</em>, <em>access plus 365 days</em> etc. The time length is by default specified in seconds, but you may also use any of these keys: years, months, weeks, days, hours, minutes and seconds.</p>
<p>You can read all about expires headers by reading Yahoo!’s <a href="http://developer.yahoo.com/performance/rules.html#expires" title="Yahoo! Best Practices for Speeding Up Your Web Site Guide" target="_blank" rel="nofollow">Best Practices for Speeding Up Your Web Site</a> guide.</p>
<p>Alternatively, read the <a href="http://httpd.apache.org/docs/2.0/mod/mod_expires.html" title="Apache mod_expires documentation" target="_blank" rel="nofollow">Apache mod_expires documentation</a>.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/how-to-set-an-expires-header-in-apache/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Enabling Search Engine Safe URLs with Apache and htaccess</title>
		<link>http://www.simonwhatley.co.uk/enabling-search-engine-safe-urls-with-apache-and-htaccess</link>
		<comments>http://www.simonwhatley.co.uk/enabling-search-engine-safe-urls-with-apache-and-htaccess#comments</comments>
		<pubDate>Mon, 08 Dec 2008 15:57:15 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[All]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[ColdBox]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Fusebox]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[httpd.conf]]></category>
		<category><![CDATA[ISAPI]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[New Brunswick]]></category>
		<category><![CDATA[None]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[search engine]]></category>
		<category><![CDATA[search engine optimisation]]></category>
		<category><![CDATA[search engine robots]]></category>
		<category><![CDATA[search engine safe]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[URL rewriting]]></category>
		<category><![CDATA[USD]]></category>
		<category><![CDATA[web applications]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=1635</guid>
		<description><![CDATA[An increasingly popular technique among websites and in particular, blogs, is the idea of making URLs search engine friendly, or safe, on the premise that doing so will help search engine optimisation. By removing the obscure query string element of a URL and replacing it with keyword rich alternatives, not only makes it more readable for a human being, but also the venerable robots that allow our page content to be found in the first place.]]></description>
			<content:encoded><![CDATA[<p>An increasingly popular technique among websites and in particular, blogs, is the idea of making <abbr title="Universal Resource Locator">URL</abbr>s search engine friendly, or safe, on the premise that doing so will help search engine optimisation. By removing the obscure query string element of a <abbr title="Universal Resource Locator">URL</abbr> and replacing it with keyword rich alternatives, not only makes it more readable for a human being, but also the venerable robots that allow our page content to be found in the first place.</p>
<p>For example, the following is WordPress&#8217; default URL configuration for a post:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http://www.domain.com/?p=1635</pre></div></div>

<p>However, buy using a URL-rewriting available in the Apache webserver, we can achieve a far better result, such as the following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http://www.domain.com/search-engine-safe-urls</pre></div></div>

<p>NB. It is also possible to achieve a similar result with an <abbr title="Internet Server Application Programming Interface">ISAPI</abbr> rewrite for Microsoft&#8217;s <abbr title="Internet Information Server">IIS</abbr> webserver, but this topic will not be included in this post.</p>
<p>To get your website working with <abbr title="search engine safe">SES</abbr> <abbr title="Universal Resource Locator">URL</abbr>s you need to enable both the <code>mod_rewite</code> module and <code>AllowOverride</code> directive in the Apache configuration file.</p>
<p>Uncomment (remove #) from the following to enable the re-write rule:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">LoadModule rewrite_module modules/mod_rewrite.so</pre></div></div>

<p>Change the <code>AllowOverride</code> directive from none to all</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;directory /&gt;
    Options FollowSymLinks
    AllowOverride all
    Order deny,allow
    Deny from all
&lt;/directory&gt;
&nbsp;
&lt;directory &quot;C:/WebRoot&quot;&gt;
    # Possible values for the Options directive are &quot;None&quot;, &quot;All&quot;,
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that &quot;MultiViews&quot; must be named *explicitly* --- &quot;Options All&quot;
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks
&nbsp;
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be &quot;All&quot;, &quot;None&quot;, or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All
&nbsp;
    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all
&lt;/directory&gt;</pre></div></div>

<p>On Apache webservers, <code>.htaccess</code> (hypertext access) is the default name of directory-level configuration files. An <code>.htaccess</code> file is placed in a particular directory, and the directives in the <code>.htaccess</code> file apply to that directory, and all its subdirectories. It provides the ability to customize configuration for requests to the particular directory. In our case, enabling search engine safe (<abbr title="search engine safe">SES</abbr>) <abbr title="Universal Resource Locator">URL</abbr>s.</p>
<p>By setting the <code>AllowOverride</code> directive to <q>All</q> in effect defers configuration settings to the <code>.htaccess</code> file.</p>
<p>An example <code>.htaccess</code> file could include the following code to rewrite the URLs:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]</pre></div></div>

<p>Search engine friendly <abbr title="Universal Resource Locator">URL</abbr>s are implemented with Rewrite engines. The rewrite engine modifies the <abbr title="Universal Resource Locator">URL</abbr> based upon a number of rewrite conditions and rules.</p>
<p>The <code>RewriteBase</code> directive explicitly sets the base <abbr title="Universal Resource Locator">URL</abbr> for per-directory rewrites. The <code>RewriteCond</code> directive defines a rule condition, so in this case handling missing files or directories. Finally, the <code>RewriteRule</code> directive is the real rewriting workhorse. In this example, we&#8217;re getting everything in the <abbr title="Uniform Resource Identifier">URI</abbr> &#8212; i.e. not including the protocol (HTTP/S) and domain name &#8212; based upon a regular expression. This is then appended to the default file reference &#8212; index.php &#8212; as a <a href="http://www.regular-expressions.info/brackets.html" title="Regular Expression: back references" target="_blank" rel="nofollow">back reference</a>. The <code>[L,QSA]</code> refers to the rule being the last rule and append any query string parameters to the default file. It is important to note that this is all done on the server side, the user will never see the website address changing in the browser&#8217;s address bar. Furthermore, simply transposing the index.php filename with your default file name &#8212; e.g. index.cfm, default.aspx &#8212; will have the same result. Indeed, the above rewrite rules are becoming a de-facto standard for web applications.</p>
<p>To fully understand <code>mod_rewrite</code> rules above, look at the <a href="http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html" title="Apache mod_rewrite documentation" target="_blank" rel="nofollow">Apache mod_rewrite documentation</a>.</p>
<p>Once you have your <abbr title="Search Engine Safe">SES</abbr> functionality in place on the webserver, it is then the responsibility of your application framework to understand the <abbr title="Universal Resource Locator">URL</abbr> construction and handle it accordingly. Fortunately, frameworks such as <a href="http://www.coldboxframework.com" title="ColdBox Framework" target="_blank" rel="nofollow">ColdBox</a> and <a href="http://www.fusebox.org" title="Fusebox Framework" target="_blank" rel="nofollow" >Fusebox</a> for ColdFusion, <a href="http://framework.zend.com" title="Zend PHP framework" target="_blank" rel="nofollow">Zend</a> and <a href="http://www.symfony-project.com" title="Symfony PHP fraemwork" target="_blank" rel="nofollow">Symfony</a> for <abbr title="PHP Hypertext Precursor">PHP</abbr>, all contain functionality to do this, but that is the subject of an entirely different post.</p>
<p>Users of web applications prefer short, neat <abbr title="Universal Resource Locator">URL</abbr>s to raw query string parameters. A concise <abbr title="Universal Resource Locator">URL</abbr> is easy to remember, and less time-consuming to type in. If the <abbr title="Universal Resource Locator">URL</abbr> can be made to relate clearly to the content of the page, then errors are not only less likely to happen, but our good friends the search engine robots are able to draw a stronger assumption of the pages&#8217; relevance and content.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/enabling-search-engine-safe-urls-with-apache-and-htaccess/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

