<?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>A K Chauhan's Blog &#187; SEF</title>
	<atom:link href="http://www.akchauhan.com/tag/sef/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.akchauhan.com</link>
	<description>PHP, MySql, JavaScript, JQuery, Prototype, Drupal, HTML, CSS and more...</description>
	<lastBuildDate>Sun, 04 Jul 2010 20:19:23 +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>Create Search Engine Friendly(SEF) websites in PHP using URL rewriting</title>
		<link>http://www.akchauhan.com/create-search-engine-friendlysef-websites-in-php-using-url-rewriting/</link>
		<comments>http://www.akchauhan.com/create-search-engine-friendlysef-websites-in-php-using-url-rewriting/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 17:40:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[SEF]]></category>
		<category><![CDATA[url rewriting]]></category>

		<guid isPermaLink="false">http://www.akchauhan.com/?p=196</guid>
		<description><![CDATA[This article is for beginners who want to learn how to create search engine friendly(SEF) URLs (or Clean URLs) for a website. I am considering that you are using Apache web server. For clean URLs we need to do URL rewriting. Note:- The “mod_rewrite” module should be enabled in Apache httpd.conf file. We can define [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_206" class="wp-caption aligncenter" style="width: 384px"><img src="http://www.akchauhan.com/wp-content/uploads/2009/04/sef-url.jpg" alt="Search Engine Friendly URL" title="Search Engine Friendly URL" width="374" height="34" class="size-full wp-image-206" /><p class="wp-caption-text">Search Engine Friendly URL</p></div>
<p>This article is for beginners who want to learn how to create search engine friendly(SEF) URLs (or Clean URLs) for a website. I am considering that you are using Apache web server. For clean URLs we need to do URL rewriting. </p>
<p><strong>Note:- The “mod_rewrite” module should be enabled in Apache httpd.conf file.</strong></p>
<p>We can define our rewrite conditions and rewrite rules in httpd.conf file but on shared hosting we don&#8217;t have access to this configuration file. So, In that case we can define these conditions and rules in &#8216;.htaccess&#8217; file.</p>
<p><strong>&#8216;.htaccess&#8217; file provide configuration settings on per-directory basic</strong>. You need to place this .htaccess file in your server’s root directory.</p>
<p>We need to put the following code in our .htaccess file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>IfModule mod_rewrite<span style="color: #339933;">.</span>c<span style="color: #339933;">&gt;</span>
  RewriteEngine on
&nbsp;
  <span style="color: #666666; font-style: italic;"># Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
</span>  RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>f
  RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>d
  RewriteRule ^<span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ index<span style="color: #339933;">.</span>php?q<span style="color: #339933;">=</span>$<span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#91;</span>L<span style="color: #339933;">,</span>QSA<span style="color: #009900;">&#93;</span>
<span style="color: #339933;">&lt;/</span>IfModule<span style="color: #339933;">&gt;</span></pre></div></div>

<p>First, we are checking &#8216;mod_rewrite&#8217; module is available and loaded by Apache web server. If this module is loaded then only our rewrite rule work.</p>
<p>Then we need to &#8216;ON&#8217; RewriteEngine.  </p>
<p>In rewrite conditions we are checking few things. Like in</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>f</pre></div></div>

<p>we are checking the given file/path is not a file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>d</pre></div></div>

<p>we are checking the given file/path is not a directory. </p>
<p>If the above two conditions satisfy than only our rewrite rule apply to the given URL. We are doing this because if browser request any resource like JavaScript or CSS or Image files than we don&#8217;t want to rewrite those URLs. If any path/URL which does not exist on our server will be rewrite by RewriteRule. All such type of requests will be handled by index.php file.</p>
<p>For example, If we write<br />
<strong><br />
http://www.websitename.com/article/25</strong> (This URL is not exist on our server)</p>
<p>in address bar then this URL is rewritten as </p>
<p><strong>http://www.websitename.com/index.php?q=article/25</strong></p>
<p>Now, the only thing we need to do is to handle all the requests properly in our index.php file. We can create a file &#8216;url.inc&#8217; for handling all the conditions and include it in top of our index.php file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*url.inc file*/</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'article'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> isnumeric<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'article.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>some other condition<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// include some other file</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If any of the condition mentioned in the &#8216;url.inc&#8217; file not match then index.php files content will execute.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*index.php file*/</span>
&nbsp;
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url.inc'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;This is the index.php file.&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.akchauhan.com/create-search-engine-friendlysef-websites-in-php-using-url-rewriting/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
