create user friendly url containing username in php
<p>I want to pass username in url just like what other social networking sites like facebook do.
The url should be like : <code>www.mysite.com/username</code></p>

<p>Also I want to be able to access directories if the value at place of username is a directory name.</p>

<p>Ex. <code>www.mysite.com/downloads</code> will take to the <code>downloads</code> directory.</p>

<p>For this what I can do is to first check where the value passed is a valid directory name. If yes, access directory else check for whether user exists with the username passed.</p>

<p>for this what I have done is, created a <code>.htaccess</code> file in root directory containing</p>

<pre><code>RewriteEngine On
RewriteRule ^ index.php
</code></pre>

<p>and checking for the url</p>

<pre><code>// requested url
$request = $_SERVER['REQUEST_URI'];
// trim last / if available
$url = rtrim($request,'/');
// explode url and save in $ar array
$ar = explode('/',$url);

if(sizeof($ar) &gt; 2)
{
  header('location : error/?error=1');
} else {
  $user = $ar[1];
}
</code></pre>

<p>The if statement above is to check for passed parameter is one or more.
Ex. <code>www.mysite.com/username/post</code> will result in an invalid url</p>

<p>But by this code, I couldn't access my directory <code>www.mysite.com/downloads</code>
what is the easy way to do this.</p>

<blockquote>
  <p>Edit 2 :</p>
</blockquote>

<p>I want the url <code>www.mysite.com/username</code> to pass <code>username</code> as variable <em>if is not a directory or file</em> to <code>index.php</code> in <code>myProfile</code> directory where it is access as <code>$user = $_GET['u'];</code></p>

<p>updated <code>.htaccess</code></p>

<pre><code># Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
Options All -Indexes

# Timezone
SetEnv TZ Asia/Kolkata

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /myProfile/index.php?u=$1 [L,QSA]
</code></pre>