Page 1 of 1

Virtual Directories + Forwarding....

Posted: Mon Nov 14, 2005 10:32 am
by ed209
Hi,

I am trying to add the following functionality to my web site:

When someone creates an account on my site, I give them an ugly url like this:

www.domianname.com/directory/?theirUserName

as you'll agree, this is rubbish! What I'd like to do is give them a url like this

www.domianname.com/theirUserName

but without creating that directory. Instead, is it possible to receive this virtual directoy (/theiUserName) as a variable and look it up in a database? I do that in a round about way already but want to make it neater - i.e. remove the '?' to get the variable. Is this possible?

thanks for your help.

Posted: Mon Nov 14, 2005 12:56 pm
by trukfixer
yep. definitely possible.
I did that with a members area at cashglow.

First, you need a .htaccess , and your server needs to have mod_rewrite enabled

Code: Select all

ErrorDocument 404 /refer/index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) /refer/index.php [L]
what the above does, it sets a ErrorDocument 404 (because the folder domain.com/username does not exist)
then the rewrite rule kicks in , taking the filename request (everything after domain.com in the url.. E.G. /username)
and then rewrites it to the error_document page , setting the REQUEST_URI to teh value extracted

then the php code:

Code: Select all

$path_data = $_SERVER['REQUEST_URI'];//will contain whatever came after the .com including "index.php" or whatever 
$part=explode("/",$path_data); //explodes the REQUEST_URI on / 
$name=$part[2];//takes the 2nd element (because thh rewrite makes request_URI into /refer/index.php/username as shown //above, therefore username is the 3rd (element [2]) element of the exploded array) 

//now you can just take $name and apply it to whatever memmber name check you wish , just like doing $name = $_GET['name'];
That's the basics of it..hope it helps.

Bri!

Posted: Tue Nov 15, 2005 6:10 am
by ed209
Hi,

Thanks for your reply. It took me a while but I got it to work eventually. I found and edited my .htaccess file ( in my web site root directory public_html) and added the line:

Code: Select all

ErrorDocument 404 /404.php
to it. Then I created the 404.php with a combination of your code and the bit I was using to access the MySQL database and it works perfectly!

The problem I had is that the line breaks on my Mac were different to the ones on the server so all the content of the .htaccess file was on one line.

Thanks for your help.
ed.