Using url variables without question marks

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
mabbus
Forum Newbie
Posts: 1
Joined: Tue Apr 13, 2010 9:52 am

Using url variables without question marks

Post by mabbus »

Hi,
I'm building an application for a company and they wish to have a front page with a userbox on top of the site and the user's info. Instead of having to open a directory for each user, I'd rather pass the username on the url like this: "http://yourdomain.com/user". This way i can grab the user and look for his info on the database. I've read some posts online about editing the htaccess file but nothing seems to work for me. :banghead:

Hope somebody out there knows the answer.....

Peace!
mrcoffee
Forum Commoner
Posts: 31
Joined: Tue Nov 10, 2009 3:03 pm
Location: Wyoming, USA

Re: Using url variables without question marks

Post by mrcoffee »

Hey there.

The following works for me. Maybe you can use it as a start.

.htaccess
[text]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
[/text]

Then in index.php (in the same directory as .htaccess), the variable $_GET['q'] will be everything after the first slash.

Here are some examples:
yourdomain.com/user34 will load index.php with $_GET['q'] = 'user34'
yourdomain.com/user34/edit will load index.php with $_GET['q'] = 'user34/edit' (you'd need to explode by '/')
yourdomain.com/user34?view=something OR yourdomain.com/user34&view=something will be $_GET = array('q'=>'user34' , 'view'=>'something)
yourdomain.com/images/img1.png, if it exists, will retrieve the image.

Hope that helps.
Post Reply