开发者

Technique to create user friendly URLs [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

H开发者_开发知识库ow 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


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

RewriteEngine On
RewriteRule ^/profile/username/([\d]+)$ profile.php?id=$1


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜