<?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>Vision Master Designs &#187; SEO</title>
	<atom:link href="http://visionmasterdesigns.com/category/website-optimization/seo/feed/" rel="self" type="application/rss+xml" />
	<link>http://visionmasterdesigns.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 29 Oct 2009 20:00:15 +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>Tutorial : Creating &#8220;Pretty SEO URLS&#8221; using Apache&#8217;s mod_rewrite</title>
		<link>http://visionmasterdesigns.com/tutorial-creating-pretty-seo-url-using-apache-mod-rewrite/</link>
		<comments>http://visionmasterdesigns.com/tutorial-creating-pretty-seo-url-using-apache-mod-rewrite/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 09:11:30 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[PHP & MySQL]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[Website Optimization]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[mod_rewrite]]></category>

		<guid isPermaLink="false">http://www.visionmasterdesigns.com/?p=323</guid>
		<description><![CDATA[Imagine this, You have a dynamic site powered by PHP/MySQL, its doing ok, you have submitted your website URL to all the known search engines, but still your site doesn&#8217;t seem to get much traffic from the search engines&#8230;ever wondered why ? Well firstly, search engines have billions of pages to search. Imagine your link [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 0 0 0.6em 0.6em;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fvisionmasterdesigns.com%2Ftutorial-creating-pretty-seo-url-using-apache-mod-rewrite%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fvisionmasterdesigns.com%2Ftutorial-creating-pretty-seo-url-using-apache-mod-rewrite%2F&amp;source=rowoot&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Imagine this, You have a dynamic site powered by PHP/MySQL, its doing ok, you have submitted your website URL to all the known search engines, but still your site doesn&#8217;t seem to get much traffic from the search engines&#8230;ever wondered why ?</p>
<p>Well firstly, search engines have billions of pages to search. Imagine your link to be something like <span class="code">www.yourdomain.com/index.php?page=aboutme</span>, its tough for any search engine to comprehend any keywords from the link.<br />
But how about <span class="code">www.yourdomain.com/page/about-me.html</span> ? These links are static Links, SEO Friendly Links, &#8220;Pretty Links&#8221;.</p>
<p>So today we are going to learn how to convert your ugly links into pretty links.</p>
<h4>Index</h4>
<ol>
<li>Step 1 : Enable mod_rewrite in httpd.conf</li>
<li>Step 2 : Create A Simple PHP file</li>
<li>Step 3 : .htaccess file<br />
Method 1 &#8211; For only a single Variable<br />
Method 2 &#8211; For 1 and more than 1 Variable(s)
</li>
<li>Conclusion</li>
</ol>
<p>Ugly URLs<br />
<a href="http://www.visionmasterdesigns.com/demo/seo/test.php?page=page1" target="_blank">http://www.visionmasterdesigns.com/demo/seo/test.php?page=page1</a><br />
<a href="http://www.visionmasterdesigns.com/demo/seo/test.php?page=page1&amp;var=foo" target="_blank">http://www.visionmasterdesigns.com/demo/seo/test.php?page=page1&amp;var=foo </a></p>
<p>Pretty URLs<br />
<a href="http://www.visionmasterdesigns.com/demo/seo/page1.html" target="_blank">http://www.visionmasterdesigns.com/demo/seo/page1.html</a><br />
<a href="http://www.visionmasterdesigns.com/demo/seo/page1/foo.html" target="_blank">http://www.visionmasterdesigns.com/demo/seo/page1/foo.html</a></p>
<p>Click <a href="http://www.visionmasterdesigns.com/demo/seo/seo.zip">Here</a> to download the script files.<br />
<span id="more-323"></span></p>
<h2 align="center">Step 1 : Enable mod_rewrite in httpd.conf</h2>
<p>Normally webservers already have mod_rewrite enabled. If it is not enabled, then you can request your host to enable it.</p>
<p>If you are going to try this tutorial in your local machine. Do check whether <span class="code">mod_rewrite</span> module has been enabled in Apache. Do the following.</p>
<ol>
<li>Navigate to your apache folder and open httpd.conf</li>
<li>Search for
<pre lang="apache">#LoadModule rewrite_module modules/mod_rewrite.so</pre>
</li>
<li>Remove the commenting character i.e #, then save the file.</li>
<li>Restart the Server</li>
</ol>
<h4>About the .htaccess file</h4>
<p>You must have seen or heard about <span class="code">.htaccess</span> file somewhere.</p>
<p><strong>Quote from wikipedia :</strong><br />
In the Apache web server, .htaccess (hypertext access) is the default name of directory-level configuration files. A .htaccess file is placed in a particular directory, and the directives in the .htaccess file apply to that directory, and all subdirectories thereof. It provides the ability to customize configuration for requests to the particular directory.</p>
<p>Normally the .htaccess file must be present in the directory where your script is stored.<br />
For Example : If my script is<br />
<span class="code">myserver.dev/prettyurl/test.php</span><br />
 then my <span class="code">.htaccess</span> file will be saved in<br />
  <span class="code">C:\server\www\myserver.dev\public_html\prettyurl\.htaccess</span>  <b>*</b></p>
<p>*This is applicable if you have followed &#8211;<br />
<a href="http://www.visionmasterdesigns.com/2008/07/tutorial-installing-apache-php-5-mysql-5-phpmyadmin-windows-vista-xp/">Tutorial : Installing Apache, PHP 5, MySQL 5 &#038; PhpMyAdmin 2.11 in Windows Vista/XP</a></p>
<h2 align="center">Step 2 : Create A Simple PHP file</h2>
<p>Lets create a simple PHP file. PHP allows us to retrieve values from the URL iself using $_GET function.</p>
<p>For Example :<br />
<span class="code">http://myserver.dev/prettyurl/test.php?page=mypagename</span>.<br />
$_GET['page'] would give the value of page in the above url.</p>
<p><span class="code">http://myserver.dev/prettyurl/test.php?page=mypagename&#038;var=secondvar</span>.<br />
We can have retrieve quite a lot of data from the URLs by using multiple $_GET functions. In the above URL we can have $_GET['page'] and $_GET['var']</p>
<pre class="brush: php">
&lt;?php

//test.php
//storing the retrieved val from URL into a variable
$p = $_GET[&#039;page&#039;];
$q = $_GET[&#039;var&#039;];

switch($p) {
    case &#039;page1&#039;:
    // if $p = page1 i.e url was something like test.php?page=page1
  		switch($q) {
		case &#039;boo&#039;:
		//if $p=boo then url is somehting like test.php?page=page1&amp;amp;amp;var=boo
		echo &quot;&lt;h2&gt;This is page1 And BOO&lt;/h2&gt;&lt;br /&gt;&quot;;
		break;
		case &#039;foo&#039;:
		echo &quot;&lt;h2&gt;This is page1 And FOO&lt;/h2&gt;&lt;br /&gt;&quot;;
		break;
		default:
		// if $p = page1 i.e url was something like test.php?page=page1
		echo &quot;&lt;h2&gt;This is page1&lt;/h2&gt;&quot;;
		break;
		}
    break;
    case &#039;page2&#039;:
    // if $p = page2 i.e url was something like test.php?page=page2
    echo &quot;&lt;h2&gt;This is page2&lt;/h2&gt;&quot;;
    break;
    case &#039;page3&#039;:
    // if $p = page3 i.e url was something like test.php?page=page3
    echo &quot;&lt;h2&gt;This is page3&lt;/h2&gt;&quot;;
    break;
    case &#039;page4&#039;:
    // if $p = page4 i.e url was something like test.php?page=page4
    echo &quot;&lt;h2&gt;This is page4&lt;/h2&gt;&quot;;
    break;
    default:
    // if page != any of the above, then default is executed
    echo &quot;&lt;h2&gt;This is the Default Page&lt;/h2&gt;&quot;;
	break;
}
?&gt;
</pre>
<p>Save the file as <span class="code">test.php</span>.</p>
<p>Test out the script, if we access<br />
<a href="http://myserver.dev/prettyurl/test.php?page=page1">http://myserver.dev/prettyurl/test.php?page=page1</a><br />
the result will be &#8216;This is page1&#8242;.<br />
Similarly <a href="http://myserver.dev/prettyurl/test.php?page=page2">http://myserver.dev/prettyurl/test.php?page=page2</a><br />
will show the output &#8216;This is page2&#8242; and so on.</p>
<p>If we access<br />
<a href="http://myserver.dev/prettyurl/test.php?page=page1&#038;var=foo">http://myserver.dev/prettyurl/test.php?page=page1&#038;var=foo </a><br />
the output will be &#8216;This is page1 And foo&#8217;</p>
<p>This sample code in PHP is to just show you how a general linking system works. It shows how $_GET works and how we can add data in the URL&#8217;s using &#8216;&#038;&#8217; operator.</p>
<h2 align="center">Step 3 : .htaccess file</h2>
<p>There are different methods to create &#8220;Pretty URLS&#8221;. I`ll just discuss the most common and the most optimized ones.</p>
<p>Ok ! Firstly our local server won`t have a .htaccess file, so we will have to create one. Fire up notepad, then type the following code.<br />
Save the file as <span class="code">.htaccess</span>. Place it in the directory where you have saved your <span class="code">test.php</span> script.</p>
<h4>Method 1 &#8211; For only a single Variable</h4>
<p>Consider our dynamic URL to be<br />
<span class="code">http://myserver.dev/prettyurl/test.php?page=page2</span><br />
and we want our pretty url to look something like<br />
<span class="code">http://myserver.dev/prettyurl/page2.html</span> </p>
<pre lang="apache">
#.htaccess code
RewriteEngine On
RewriteRule ^(.*)\.html$ test.php?page=$1 [L]
</pre>
<p>Let me explain what every line does.</p>
<ul>
<li><span class="code">RewriteEngine On</span><br />
This tells to start the Rewrite Engine.</li>
<li><span class="code">RewriteRule ^(.*)\.html$ test.php?page=$1 [L]</span><br />
The basic syntax of<br />
RewriteRule pattern target_url [flag,flag,flag,…]</p>
<ol>
<li>pattern is <span class="code">^(.*)\.html$</span><br />
() &#8211; Parentheses allow you to group several characters as a unit and also to capture the results of a match for later use. The ability to treat several characters as a unit is extremely useful in pattern matching. </p>
<p><span class="code">(.*)</span> &#8211; Matches anything.<br />
Eg: http://myserver.dev/prettyurl/page1.html where (.*)\.html matches page1.html
</li>
<li>
target_url is <span class="code">test.php?page=$1 </span><br />
$1 is a variable where $1 = (.*) from the pattern.
</li>
<li>
flag is <span class="code">[L]</span>.<br />
There are various flags. [L] means, If there is a match with a particular rewrite rule, It has to be the last rewrite rule to be executed.
</li>
</ol>
</li>
</ul>
<p>Now lets test run our script. If the dynamic link is like<br />
<a href="http://myserver.dev/prettyurl/test.php?page=page1">http://myserver.dev/prettyurl/test.php?page=page1</a><br />
then your pretty link will be<br />
<a href="http://myserver.dev/prettyurl/page1.html">http://myserver.dev/prettyurl/page1.html</a><br />
Similarly for page=page2 the link will be page2.html ,page=page3 the link will be page3.html.</p>
<p>Phew ! That took a lot of time. If you are able to see the same output for both the ugly links and the pretty links, then you Rewrite rules are working perfectly.</p>
<h4>Method 2 &#8211; For 1 and more than 1 Variable(s)</h4>
<p>Now what if your dynamic URL was like<br />
<span class="code">http://myserver.dev/prettyurl/test.php?page=page1&#038;var=foo</span>.<br />
For the above dynamic URL we want our static URL to look something like<br />
<span class="code">http://myserver.dev/prettyurl/page1/foo.html</span>.<br />
We want the first variable to act as a directory, while the second one should be the file name.</p>
<p>Note that previously page=page1 had become &#8216;page1.html&#8217; since there was just one variable. But now we have 2 variables (page=page1&#038;var=foo) so we the structure should be like &#8216;page1/foo.html&#8217;</p>
<p>We have to write a new RewriteRule to support 2 variables.<br />
<strong>Remember : As your variables increase, the new Rewrite rules come before the previous ones.</strong></p>
<p>We are basically extending Method 1 by adding another RewriteRule to support 2 variables as input.</p>
<pre lang="apache">
RewriteEngine On
RewriteRule ([^/]+)/(.*)\.html$ test.php?page=$1&#038;var=$2 [L]
RewriteRule ^(.*)\.html$ test.php?page=$1 [L]
</pre>
<p>Let me Explain what the new line does.<br />
<span class="code">RewriteRule ([^/]+)/(.*)\.html$ test.php?page=$1&#038;var=$2 [L]</span></p>
<ol>
<li>pattern is <span class="code">([^/]+)/(.*)\.html$</span><br />
<span class="code">([^/]+)</span> &#8211; Which has the effect of matching any characters up to a slash or the end of the string, whichever comes first.<br />
<span class="code">(.*)</span> &#8211; Matches anything</p>
<p><span class="code">([^/]+)/(.*)\.html$</span> &#8211; characters up to slash &#8216;/&#8217; any characters &#8216;.html&#8217;<br />
Eg: page1/foo.html where ([^/]+) matches page1 and (.*)\.html matches foo.html
</li>
<li>
target_url is <span class="code">test.php?page=$1&#038;var=$2 </span><br />
Every variable i.e $1 and $2 matches with the unit inside the paranthesis.<br />
$1 = ([^/]+)<br />
$2 = (.*)
</li>
<li>
flag is <span class="code">[L]</span>.<br />
There are various flags. [L] means, If there is a match with a particular rewrite rule, It has to be the last rewrite rule to be executed.
</li>
</ol>
<p>You can test it out<br />
For all dynamic links like<br />
<a href="http://myserver.dev/prettyurl/test.php?page=page1&#038;var=foo">http://myserver.dev/prettyurl/test.php?page=page1&#038;var=foo </a><br />
will have static URL&#8217;s like<br />
<a href="http://myserver.dev/prettyurl/page1/foo.html">http://myserver.dev/prettyurl/page1/foo.html</a></p>
<h2 align="center">Conclusion</h2>
<p>(Method 1 &nbsp; Method 2)<br />
Thus for all dynamic links like</p>
<p>http://myserver.dev/prettyurl/test.php?page=pagex</p>
<p>will have static URL&#8217;s like</p>
<p>http://myserver.dev/prettyurl/pagex.html</p>
<p>(Method 2)<br />
For all dynamic links like</p>
<p>http://myserver.dev/prettyurl/test.php?page=pagex&#038;var=foo</p>
<p>will have static URL&#8217;s like</p>
<p>http://myserver.dev/prettyurl/pagex/foo.html</p>
<p>This was quite a basic tutorial. It is tough to understand the string matching patterns initially. But the logic is very simple. I hope the tutorial wasn&#8217;t tough to understand. If you guys have any doubts, feel free to comment here, I`ll try to solve them. Cya</p>
<p>Click <a href="http://www.visionmasterdesigns.com/demo/seo/seo.zip">Here</a> to download the files.</p>
]]></content:encoded>
			<wfw:commentRss>http://visionmasterdesigns.com/tutorial-creating-pretty-seo-url-using-apache-mod-rewrite/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
