0

My url like this

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

I want to make like a facebook as:

mydomain.com/rbkdon3

Note:Username is the combination of alphabets+numbers.

What should i need to be done in routes.php file to achieve my requirement.

Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
SP Singh
  • 15
  • 7

2 Answers2

0

you must use apache rewrite module and use .htaccess first

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]

and delete index.php from applications/config/config.php

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

and route with

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

regex is updated

dhidy
  • 342
  • 1
  • 3
  • 10
  • thnks for your answer, is this code effect on my other controllers in the project..????? b'coz of $route['(:any)'] = "user/$1"; – SP Singh Mar 14 '14 at 05:02
  • edited with regex, when that value containing number and string use user/$1 – dhidy Mar 14 '14 at 05:13
0

By this way you can achieve it.

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

Creating this route dynamically is bit complex

We have to create this route only if user exits in the database, so it will not affect other controllers. Write below code in your 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]"
}

Note: Remove index.php from url using .htaccess file.