3

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

Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
  • Are you looking how to do something similar in your own code, or to modify SO? (Suggest rapid clarification, otherwise it will be taken as suggestion for SO, which belongs on User Voice.) – Richard Apr 22 '09 at 09:29
  • in my code, url was to get things easy –  Apr 22 '09 at 09:51
  • Check out this [mod_rewrite tutorial](http://www.workingwith.me.uk/articles/scripting/mod_rewrite) – Ólafur Waage Apr 22 '09 at 09:29

3 Answers3

5

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.

Yang
  • 8,580
  • 8
  • 33
  • 58
Laurent
  • 5,953
  • 14
  • 43
  • 59
4

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
  • 82,642
  • 24
  • 155
  • 189
  • hey, echo $_GET['u']; echo "
    "; $count = strlen($_SERVER['PHP_SELF']); echo "first count; $count
    "; $nme = $count - strlen(pete); echo "second count; $nme
    "; echo substr($_SERVER['PHP_SELF'],$nme); ?
    –  Apr 22 '09 at 09:40
  • also the above does only this http://stackoverflow.com/user/me/ and not this http://stackoverflow.com/user/me notice the "/" –  Apr 22 '09 at 09:50
0
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
Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198