0

我的网址这样如何笨使用路由

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

我要作出这样一个网站的创建Facebook的URL类型用户名是字母+数字的组合。

我应该在routes.php文件中做些什么才能达到我的要求。

+0

可能重复的[用PHP重写URL](http://stackoverflow.com/questions/16388959/url-rewriting-with-php) –

+0

但我想通过遵循路由在codeigniter –

回答

0

必须使用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

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

和路由删除index.php

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

正则表达式被更新

+0

thnks for你的答案,这是对我的项目中的其他控制器的代码效果.. ????? $ route ['(:any)'] =“user/$ 1”的b'coz;用正则表达式编辑的 –

+0

,当包含数字和字符串的值使用user/$ 1 – dhidy

0

通过这种方式,你可以实现它。

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

动态创建这条路线是有点复杂

我们必须创建这条路线只有当数据库中的用户退出,所以不会影响到其他控制器。在您的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]" 
} 

注意:使用.htaccess文件从url中删除index.php。