如何在codeigniter中使用路由创建Facebook类型url


How to create Facebook type url in codeigniter using routing

我的url是这样的

"mydomain.com/index.php/user/rbkdon3"

我想把facebook做成:

mydomain.com/rbkdon3

注意:用户名是字母+数字的组合。

我需要在routes.php文件中做什么来实现我的要求

必须使用apache重写模块,并首先使用。htaccess

RewriteEngine on
RewriteCond $1 !^(index'.php|(.*)'.swf|forums|images|css|downloads|jquery|js|robots'.txt|favicon'.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]

,从applications/config/config.php中删除index.php

$config['index_page'] = "index.php";

和route with

$route['(^(?=[^'s]*?[0-9])(?=[^'s]*?[a-zA-Z])[a-zA-Z0-9]*$)'] = "user/$1";`

regex被更新

通过这种方式可以实现。

$route['(:any)'] = 'user/'.$username;

动态创建路由有点复杂

我们必须只在user退出数据库时创建此路由,因此它不会影响其他控制器。在你的routes.php

中编写以下代码
$username = explode('/', $_SERVER['REQUEST_URI']);
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->where('username', $username[1] ); //check user name 
$query = $db->get( 'tbl_users' ); //table name in which user exits
if($query->num_rows > 0){ //if exits the create routing
    $route['(:any)'] = 'user/'.$username[1]; // here you can define any controller and function name as required for the demo I have given this "'user/'.$username[1]"
}