Custom Profile Page like Facebook in PHP
I have found the answer from
.htaccess RewriteRule to path without changing URL
# Tell PHP that the mod_rewrite module is ENABLED.
SetEnv HTTP_MOD_REWRITE On
RewriteEngine on
RewriteBase /
#For internal pages(rewrite "/page.php?name=about us" to "AboutUs")
RewriteCond %{QUERY_STRING} ^name=([^&\s]+)$
RewriteRule ^(?:page\.php|)$ /%1? [R=301,L]
RewriteCond %{QUERY_STRING} ^name=([^&\s]+)&name=([^&\s]+)$
RewriteRule ^(?:page\.php|)$ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\s\/]+)/?$ page.php?name=$1&r [L,QSA]
How to make user profile link like facebook for my web page
Check this article out to get started:
http://yiitutorials.net/easy/easy-url-rewriting-with-yii
You will bascially be storing the users custom URL in the database. So an example URL would be http://example.com/user/userurl
In your main config you could set up a rule like:
user/<customurl:.+> => 'user/view' //second part is the controller/action
That rule defines that a URL with 'http://example.com/user/', the part after the slash can be accessed by a GET variable with the name 'customurl'. You can then access the users custom URL like so:$_GET['customurl'];
And query the user record something like so:$user = User::model()->find("customurl = '".$_GET['customurl']."'");
As mentioned below, here is an example from a real website. In this example the URL looks like so: http://website.com/blog/{post_title}/{post_id}So the rule for this would be something like:
london-blog/<post_title:.+>/<post_url:.+> => 'blog/viewpost'
So in our blog controller, we have an action called viewpost (see above how the rule is pointing to this controller/action?), which would look something like:public function actionViewpost(){
$blogpost = Blog::model()->find("post_title = '".$_GET['post_title']."'");
...
}
So any URL that has the format london-blog/some_value/some_value will point to the controller action as specified in the config. You can then access the some_value parts using the variable names defined in the config (the bits in the < :.+>)Hope that helps!
Custom Profile URL like site url/username Codeigniter
Finally, I have created the URL as I wanted such as example.com/username. Here I am posting it as a help for others.
For more
I used standard MVC
This type of vanity URL involve URI routing, So add below code for your "application/config/routes.php"
if($handle = opendir(APPPATH.'/controllers'))
{
while(false !== ($controller = readdir($handle)))
{
if($controller != '.' && $controller != '..' && strstr($controller, '.') == '.php')
{
$route[strstr($controller, '.', true)] = strstr($controller, '.', true);
$route[strstr($controller, '.', true).'/(:any)'] = strstr($controller, '.', true).'/$1';
}
}
closedir($handle);
}
// these are the /username routes
$route['([a-zA-Z0-9_-]+)'] = 'controller/index/$1';
Then in your controller write you can get the username from GET.function index($username = '')
{
echo $username;
//your code here
}
Related Topics
The Behaviour of the or Operator in PHP
PHP Script to Log the Raw Data of Post
Creating a File Progress Bar in PHP
How to Extract Frames of an Animated Gif with PHP
Base64 Over Http Post Losing Data (Objective-C)
Compare Object Properties and Show Diff in PHP
How to Access Magento Customer's Session from Outside Magento
How to Create a PHP Function That I Can Call Without Parentheses
What Is the Js Equivalent to the PHP Function Number_Format
How to Select Year and Month from the Created_At Attributes of Database Table in Laravel 5.1
Yii Framework 2.0 Login with User Database
PHP Utf-8 to Windows Command Line Encoding
Force Cache Refresh After Deployment
Visits Counter Without Database with PHP