views:

228

answers:

4

I've noticed many sites are able to use a username or page title as an action. How is this done?

For example instead of www.example.com/users/my_username (where the users action is generic and responsible for fetching user data) how could I make this www.example.com/my_username?

Thanks very much.

A: 

With mod_rewrite you could write a rule that redirects www.example.com/user/my_username (or without the user) to www.example.com/user/?name=my_username.

middus
A: 

If you're using Apache, use mod_rewrite to send all requests to a controlling php script.

For instance, your .htaccess file could contain:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Then the index.php could:

if (!empty($_GET['url'])) {
  // parse the url for the username

}

This is how many script accomplish dynamic uri's.

webbiedave
A: 

All modern frameworks follow router ideology. So for this task you just need to write yet another route.

How to do this - is a specific task for particular framework.

zerkms
A: 

In CodeIgniter it would be a route like zerkms said. You can define routs in /system/application/config/routes.php. Here's the CodeIgniter documentation on URI routing. Essentially you take the part of the URL (such as the username) specified in your route as a variable and can do a lookup against your db with it.

Aaron