Virtual Directories + Forwarding....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Virtual Directories + Forwarding....

Post 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.
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post 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!
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post 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.
Post Reply