<?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>ItHowTo.ro &#187; url rewriting</title>
	<atom:link href="http://www.ithowto.ro/tag/url-rewriting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ithowto.ro</link>
	<description>Blog about IT realated stuff by WSS</description>
	<lastBuildDate>Tue, 06 Jul 2010 16:35:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>URL rewriting with .htaccess</title>
		<link>http://www.ithowto.ro/2008/12/url-rewriting-with-htaccess/</link>
		<comments>http://www.ithowto.ro/2008/12/url-rewriting-with-htaccess/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 17:21:21 +0000</pubDate>
		<dc:creator>rocky</dc:creator>
				<category><![CDATA[Server-side]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[url rewriting]]></category>

		<guid isPermaLink="false">http://www.ithowto.ro/?p=33</guid>
		<description><![CDATA[If you&#8217;re looking for this, you know why you need it. But just in case you&#8217;ve stumbled upon this and are curious, URL rewriting is useful for several reasons:
1. Accessibility: most people will rather remember a path that looks like http://www.example.com/products/shoes/12/ than http://www.example.com/index.php?category=products&#38;type=shoes&#38;productid=12
2. Security: granted, this is security through obscurity, but at the very least [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re looking for this, you know why you need it. But just in case you&#8217;ve stumbled upon this and are curious, URL rewriting is useful for several reasons:</p>
<p>1. <strong>Accessibility:</strong> most people will rather remember a path that looks like <em>http://www.example.com/products/shoes/12/</em> than <em>http://www.example.com/index.php?category=products&amp;type=shoes&amp;productid=12</em></p>
<p>2. <strong>Security:</strong> granted, this is security through obscurity, but at the very least it will hide from most users the server-side technology used (PHP, ASP, etc) and the passed variable names</p>
<p>3. <strong>SEO compliance:</strong> Web crawlers work by following links and caching the content found; however, they don&#8217;t parse anything beyond the filename, so generally <em>index.php?category=products</em>, <em>index.php?category=pages</em> and <em>index.php</em> are seen as a single address, <em>index.php</em>. On the other hand, <em>/products/</em>, <em>/pages/</em> and <em>/</em> are seen as 3 separate folders, and your content will thus be indexed correctly.</p>
<p>So, you&#8217;re thinking &#8220;Yes, that would be nice&#8221; (or alternatively, &#8220;Yeah yeah,get to the point already&#8221;). What do you need?</p>
<p>Well, first of all, your pages have to be hosted on an Apache server. Since over <a href="http://news.netcraft.com/archives/2008/11/19/november_2008_web_server_survey.html#more" target="_blank">50% of the servers in the world run on Apache</a>, that shouldn&#8217;t be a problem.</p>
<p>Secondly, it should have <em>mod_rewrite</em> activated. This is a bit trickier; most hosts have it on by default, but you should probably check with your host.</p>
<p>Finally, a good grasp of <a href="http://en.wikipedia.org/wiki/Regular_expressions" target="_blank">regular expressions</a> helps, but isn&#8217;t mandatory.</p>
<p>So,  for basic URL rewriting, create a new file containing the following:</p>
<p><strong>RewriteEngine On<br />
RewriteRule ^$ index.php<br />
RewriteRule ^([a-z0-9]+)(/)?$ index.php?page=$1 [NC]</strong></p>
<p>This assumes, of course, that your paging is driven by  a variable called <em>page</em> <img src='http://www.ithowto.ro/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Save this file as <strong>.htaccess</strong></p>
<p>Please note that .htaccess is the actual extension of the file, the filename being blank. If you save it as, for instance, .htaccess.txt, it <strong>will not work</strong>.</p>
<p>Here, the first line activates mod_rewrite. The second line redirects all incoming requests to index.php. Finally, the last one takes any input, followed by a slash or not, case-insensitive, and redirects it to <em>index.php?page=[the input]</em></p>
<p>You should however be careful with this, as a user with malicious intent might exploit this. Thus, in your code, you should sanitize all the inputs, or, in case you only have a few &#8220;main&#8221; pages (such as About, Tutorials, Links and Downloads, for instance) you could modify the last rule thus:</p>
<p><strong>RewriteRule ^(about|tutorials|links|downloads)(/)?$ index.php?page=$1 [NC]</strong></p>
<p>This can be further modified, adding whatever RegEx you need. So, you use our first example, you can write</p>
<p><strong>RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/([0-9]+)(/)?$ index.php?category=$1&amp;type=$2&amp;productid=$3 [NC]</strong></p>
<p>I hope you found this useful <img src='http://www.ithowto.ro/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Update:</strong> For RegEx goodness, have a look at this article Mike Malone has posted: <a href="http://immike.net/blog/2007/04/06/the-absolute-bare-minimum-every-programmer-should-know-about-regular-expressions/">The absolute bare minimum every programmer should know about regular expressions</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ithowto.ro/2008/12/url-rewriting-with-htaccess/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
