I want to pass username in url just like what other social networking sites like facebook do.
The url should be like : www.mysite.com/username
Also I want to be able to access directories if the value at place of username is a directory name.
Ex. www.mysite.com/downloads will take to the downloads directory.
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.
for this what I have done is, created a .htaccess file in root directory containing
RewriteEngine On
RewriteRule ^ index.php
and checking for the url
// 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) > 2)
{
header('location : error/?error=1');
} else {
$user = $ar[1];
}
The if statement above is to check for passed parameter is one or more.
Ex. www.mysite.com/username/post will result in an invalid url
But by this code, I couldn't access my directory www.mysite.com/downloads
what is the easy way to do this.
Edit 2 :
I want the url www.mysite.com/username to pass username as variable if is not a directory or file to index.php in myProfile directory where it is access as $user = $_GET['u'];
updated .htaccess
# 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]
500 Internal Server Error..htaccessand a little more description.www.mysite.com/usernameis giving404error and is showing404 error page.htaccessis in root directory ie., atwww.mysite.comwhich contain a subdirectorymyProfileand anindex.htmlpage. Yeswww.mysite..com/myProfile/index.PHP?u=usernameis working from browser