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]
Try this code in /.htaccess
:
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ myProfile/index.php?u=$1 [L,QSA]
If you still get 404
then verify whether your .htaccess
is enabled or not, by putting same garbage (random) text on top of your /.htaccess
and see if it generates 500 (internal server) error or not when you visit www.mysite.com/index.php?u=username
URL in browser.