views:

338

answers:

4

Assume I have the url http://example.com/user/me, where me is the user name. When the user types the url into the address bar, I want to reveal the details of the user. I do not want urls such as http://example.com/user.php?user=me

Any help appreciated, working on LAMP

+5  A: 

One way of doing this is using apache's module called mod_rewrite. You can then rewrite the urls for /user/([a-z]+) to point to /user.php?user=$1

Search for the documentation for mod_rewrite for the details.

Laurent
+3  A: 

Check out this mod_rewrite tutorial

Ólafur Waage
+3  A: 

The most simple way would be (in .htaccess, in your server configuration or in your vhosts configuration):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

This will forward all your requests that do not match a file, a directory or a symbolic link to your index.php. In there you can inspect the request URI to determine what to do. In fact you will need some kind of dispatcher that knows how to deconstruct the URL to determine what action should be taken. In fact this could result in some MVC implementation - but that's not a requirement.

Stefan Gehrig
hey,echo $_GET['u'];echo "<BR>";$count = strlen($_SERVER['PHP_SELF']);echo "first count; $count <BR>";$nme = $count - strlen(pete);echo "second count; $nme <BR>";echo substr($_SERVER['PHP_SELF'],$nme);?
also the above does only this http://stackoverflow.com/user/me/and not this http://stackoverflow.com/user/me notice the "/"
A: 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?u=$1