Facebook Like Custom Profile Url PHP

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



Leave a reply



Submit