<?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>Ahmed Tawfik &#187; .htaccess</title>
	<atom:link href="http://www.ahmedtawfik.com/category/htaccess/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ahmedtawfik.com</link>
	<description>Life is a big school</description>
	<lastBuildDate>Wed, 14 Sep 2011 10:24:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>How to use mod_rewrite to create clean URLs</title>
		<link>http://www.ahmedtawfik.com/php/how-to-use-mod_rewrite-to-create-clean-urls/</link>
		<comments>http://www.ahmedtawfik.com/php/how-to-use-mod_rewrite-to-create-clean-urls/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 10:58:45 +0000</pubDate>
		<dc:creator>Ahmed Tawfik</dc:creator>
				<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Mod_rewrite]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ahmedphp.com/?p=609</guid>
		<description><![CDATA[Learn how to remove cruft from your URLs and give yourself greater flexibility while making things much easier for visitors and search engines. Cruft and permalinks How many times have you seen URLs like this? http://example.com/weblog/index.php?y=2000&#38;m=11&#38;d=23&#38;id=5678 http://example.com/weblog/archive/00005678.html Horrible, aren&#8217;t they? They look ugly, contain meaningless information and are hard to decipher. Wouldn&#8217;t it be nice [...]]]></description>
			<content:encoded><![CDATA[<p>Learn how to remove cruft from your URLs and give yourself greater flexibility while making things much easier for visitors and search engines.</p>
<h3>Cruft and permalinks</h3>
<p>How many times have you seen URLs like this?</p>
<pre><code>http://example.com/weblog/index.php?y=2000&amp;m=11&amp;d=23&amp;id=5678

http://example.com/weblog/archive/00005678.html

</code></pre>
<p>Horrible, aren&#8217;t they? They look ugly, contain meaningless information and are  hard to decipher. Wouldn&#8217;t it be nice if you could have URLs like this?</p>
<pre><code>http://example.com/weblog/2000/11/23/example
</code></pre>
<p>That is surely much more meaningful. The hierarchy of the directory structure  makes it easy to infer what you&#8217;re likely to see on the page: a weblog entry  from 23rd November 2000 called ‘Example’.</p>
<p>Another problem is link rot. What happens when your URLs are several years old? Will they still work? What if you ditch PHP and rewrite your site in  Python, for example? All those URLs ending in <code>.php</code> will suddenly break, and  visitors who&#8217;ve found your site in a search engine won&#8217;t get what they  bargained for.</p>
<p>Yet another problem is that search engines often avoid URLs which contain the <code>?</code> character, because they are likely to be just one node in a large database which could leave the spider going round in circles for a long time. If your URLs take that form then this may be why you haven&#8217;t found yourself in Google.<span id="more-609"></span></p>
<h3>Using <code>mod_rewrite</code></h3>
<p>This is how I&#8217;ve solved the problem. The Apache web server has a module named <a href="http://httpd.apache.org/docs/mod/mod_rewrite.html"><code>mod_rewrite</code></a>, which  allows you to create rules for rewriting requested URLs on the fly. This means  you can announce simple, clean URLs that your web server automatically converts  into an underlying format that only you need to know. Everyone looking at your  site will just get the tidy version.</p>
<p>You&#8217;ll need to edit a file called <code>.htaccess</code> at the top level of your web folder. This is where you can specify certain settings to control the way Apache accesses items in this folder and below.</p>
<p>First things first. Let&#8217;s turn on <code>mod_rewrite</code>:</p>
<pre><code>RewriteEngine On
</code></pre>
<p>This simply lets Apache know that you are going to want it to rewrite some  URLs. So let&#8217;s do just that, and create our first rule.</p>
<pre><code>RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
</code></pre>
<p>This is a bit more complicated. It specifies a regular expression and then a rewrite format. It tells Apache to look for any incoming requests whose URL  matches the expression and to rewrite them accordingly. The <code>[L]</code> tells Apache that if this rule matches, it should consider it to be the last one and stop parsing here.</p>
<p>The rule matches any URL which is formed of lower case letters, followed by a <code>/</code>, then more lower case letters and/or hyphens, and appends <code>.php</code> to the  end. It keeps track of anything wrapped in brackets <code>()</code> and refers to them  later as $1 and $2, i.e. the first and second match. So if someone visits these  URLs:</p>
<pre><code>http://example.com/weblog/archive

http://example.com/etc/colophon

</code></pre>
<p>they will be converted, so that it will be as if they were:</p>
<pre><code>http://example.com/weblog/archive.php

http://example.com/etc/colophon.php

</code></pre>
<p>It&#8217;s as simple as that. What this means is that now nobody needs to know that we are using PHP to run the site. So if everything changes to Python at some point, we could change the rule to:</p>
<pre><code>RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.py [L]
</code></pre>
<p>replacing <code>.php</code> with <code>.py</code>, so the URLs would then be converted to:</p>
<pre><code>http://example.com/weblog/archive.py

http://example.com/etc/colophon.py

</code></pre>
<p>It&#8217;s limited only by your imagination. Here are some other rules I use to control access to my weblog:</p>
<pre><code>RewriteRule ^weblog/([0-9]{4})/([0-9]{2})/([0-9]{2})/([a-z0-9\-]+)$ /weblog/index.php?y=$1&amp;m=$2&amp;d=$3&amp;n=$4 [L]
RewriteRule ^weblog/([0-9]{4})/([0-9]{2})/([0-9]{2})$ /weblog/index.php?y=$1&amp;m=$2&amp;d=$3 [L]
RewriteRule ^weblog/([0-9]{4})/([0-9]{2})$ /weblog/index.php?y=$1&amp;m=$2 [L]
RewriteRule ^weblog/([0-9]{4})$ /weblog/index.php?y=$1 [L]
</code></pre>
<p>These mean that URLs like this:</p>
<pre><code>http://wettone.com/weblog/2000/01/01/example

http://wettone.com/weblog/2000/01/01

http://wettone.com/weblog/2000/01

http://wettone.com/weblog/2000

</code></pre>
<p>are automatically converted to:</p>
<pre><code>http://wettone.com/weblog/index.php?y=2000&amp;m=01&amp;d=01&amp;n=example

http://wettone.com/weblog/index.php?y=2000&amp;m=01&amp;d=01

http://wettone.com/weblog/index.php?y=2000&amp;m=01

http://wettone.com/weblog/index.php?y=2000

</code></pre>
<p>This means I could drastically alter the way my weblog works — even  making it load a static HTML page for each day, month or year — and  nobody would be any the wiser. All the messy URLs stay behind the scenes and  the visitors only see the tidy URLs.</p>
<h3>No URL left behind</h3>
<p>What if you already have a web site which is already indexed in search engines? You can&#8217;t just change your URLs overnight because the old ones won&#8217;t exist any more. Or will they? There is a way round this with Apache&#8217;s <code>RedirectMatch</code> keyword.</p>
<p>Older versions of my site had weblog URLs like these:</p>
<pre><code>http://wettone.com/weblog/2000_01.html

http://wettone.com/weblog/index.php/2000/01

</code></pre>
<p>They have cropped up in search engines, so I want to make sure that visitors to my site get what they expected when they visit those URLs. I use these rules:</p>
<pre><code>RedirectMatch permanent ^/weblog/([0-9]{4})_([0-9]{2}).html$ /weblog/$1/$2
RedirectMatch permanent ^/weblog/index.php/([0-9]{4})/([0-9]{2})$ /weblog/$1/$2
</code></pre>
<p>These tell the browser to redirect to the new-style URL. The <code>permanent</code> keyword lets the browser know that this is a permanent change and that they don&#8217;t need to try the old URL next time.</p>
<p>You can use this technique to make sure that nobody visiting your site sees a <code>404 Not Found</code> error. Over time, search engines will revisit your site to  find these redirections, and their indexes will be updated accordingly.</p>
<h3>For more information</h3>
<p>More in-depth documentation on the Apache modules is available on their site.</p>
<p><a href="http://httpd.apache.org/docs/mod/mod_rewrite.html"><code>mod_rewrite</code></a></p>
<p><a href="http://httpd.apache.org/docs/mod/mod_alias.html"><code>mod_alias</code></a></p>
<p><a href="http://httpd.apache.org/docs/misc/rewriteguide.html">URL Rewriting Guide</a></p>
<p>Copyright from here wettone.com</p>

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li>No related posts.</li>
	</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ahmedtawfik.com/php/how-to-use-mod_rewrite-to-create-clean-urls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

