开发者

Special profile page link like www.domain.com/username

Hey guys I have a question. I wanna create a profile page for each new user, and I noticed that on facebook you could simply type in www.facebook.com/username and you get to the user's page, my question is, how can I do this without something li开发者_如何学Pythonke domain.com/users.php?useraname="username" or something like that? How can I simply make it like the facebook one?


What you are looking for is mod_rewrite. This will allow you to write PHP code that appears to the end user to be a directory on the server (such as www.facebook.com/user.php?username into www.facebook.com/username.)

An introduction to them with PHP can be found here: http://wettone.com/code/clean-urls

Please note you will need to enable it on your server. That should be possible in the .htaccess file if you're running an Apache server.


This is not a complete answer since I'm NOT a php guy

What you're looking for a RESTful urls, mostly you can get urls like that on your web app if you use a framework that supports restful urls

See this SO question:
REST-style URLS and PHP

See this article:
http://blog.garethj.com/2009/02/building-a-restful-web-application-with-php/

Search google and Search SO with google


This can’t be done with PHP alone. It’s the web server that needs to know how to handle these kind of request first.

Because, to put it simply, a web server just takes the requested and tries to map it onto a file in the file system below the document root directory. And if it can’t find an appropriate file, it returns an 404 error code.

Now there is some kind of URL rewriting mechanism for almost every web server software. In case of Apache as the most popular web server software out there, there is mod_rewrite that allows URL rewriting based on rules. In this case the following could enable /users.php?username=username being also accessible through /username:

RewriteEngin on
RewriteRule ^[a-z]+$ index.php?username=$0


Options +FollowSymLinks
RewriteEngine On
RewriteBase /php/profile RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(.)$ ./profile.php

http://exapmle.com/users/waqar.alamgir

in $_SERVER['REQUEST_URI'] you will see users/waqar.alamgir

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜