1

Possible Duplicate:
How to create friendly URL in php?

I have read a lot of content on Google about turning

profile.php?id=24 into something more user friendly.

How would I go about redirecting a link like

www.site.com/profile/username

to

profile.php?id=userid (find from username)

Thanks

Community
  • 1
  • 1
sark9012
  • 5,485
  • 18
  • 61
  • 99
  • 2
    possible duplicate of [How to create friendly URL in php?](http://stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php) and [How can I make the URL search engine friendly?](http://stackoverflow.com/questions/1653520/how-can-i-make-the-url-search-engine-friendly) and [tons of others](http://stackoverflow.com/search?q=php+user+friendly+url&submit=search). Please use the search before you ask a question. – Felix Kling Aug 25 '11 at 11:21
  • 1
    Actually the initial link is generally more like `www.site.com/profile/userid/username`. The username is just there for convenience and isn't used in the URL translation. Look at how SO works for example. – user703016 Aug 25 '11 at 11:21
  • You could've done some research. The answer to this question is found 5.3 million times on Google already – dtech Aug 25 '11 at 11:24
  • I always trust the answers of users on this website more than random articles found on Google. – sark9012 Aug 25 '11 at 11:27
  • @Cicada, I understand what you mean about the userid and username being together. Makes a lot of sense rather than running a query to find the id from the username. – sark9012 Aug 25 '11 at 11:28

4 Answers4

1

This can be achieved with the Apache mod_rewrite RewriteEngine. An example .htaccess:

RewriteEngine On
RewriteRule ^/profile/username/([\d]+)$ profile.php?id=$1
dtech
  • 13,741
  • 11
  • 48
  • 73
0

You can do that with apache rewrite rules.

Apache will internally rewrite a URL like /profile/username to profile.php?username=username.

Example: (place this in a .htaccess in the same directory than profile.php)

RewriteEngine On
RewriteRule ^/profile/(.*)$ profile.php?username=$1

If you profile.php script doesn't accept a username parameter, you could also include the user id in the user friendly url:

RewriteEngine On
RewriteRule ^/profile/(.*)-(\d+)$ profile.php?id=$2

This will rewrite urls like /profile/username-id to profile.php?id=id

See apahe mod_rewrite documentation.

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
0

This is done by creating a RewriteRule in a .htaccess file, of by defining rules in httpd.conf. This works on Apache. I'm, not sure how to do this on other servers.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

When your users sign up, use a PHP or CGI script to make a file called "Username.txt" and store it in a folder called Profiles (as you have it). And inside the text file, make it generate the number (counting up or hashed?) Or use a rewrite service in Google or Apache.

U4iK_HaZe
  • 171
  • 7