Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove controller name from the URL in codeigniter?

I have controller named “profile” and the URL for user profile is www.example.com/profile/user

I am already using the rerouting of codeigniter in the routes file

$route['profile/(:any)'] = "profile/index"; 

what I am looking is to remove the profile controller name from the URL so it will be SEO and user friendly.

e.g

www.example.com/user

any suggestions?

like image 818
Imran Ahmed Avatar asked Aug 03 '26 23:08

Imran Ahmed


2 Answers

You may try any one of these

// url could be yourdomain/imran
$route['(:any)'] = 'profile/index/$1';
// url could be yourdomain/10
$route['(:num)'] = 'profile/index/$1';
// url could be yourdomain/imran10
$route['([a-zA-Z0-9]+)'] = "profile/index/$1";

Your class may look like this

class Profile extends CI_Controller {

    public function index($id)
    {
        // $id is your param
    }
}

Update : (Be careful)

Remember that, if you have a class Someclass and you use url like yourdomain/Someclass then this will be routed to profile/index/$1 if you have $route['(:any)'] or $route['([a-zA-Z0-9]+)'].

like image 125
The Alpha Avatar answered Aug 06 '26 16:08

The Alpha


You can use this in a route file:

$route['(:any)'] = "controller_name/$1";
$route['(:any)/(:any)'] = "controller_name/$1/$1";
$route['(:any)/(:any)/(:any)'] = "controller_name/$1/$1/$1";
like image 29
Anowar Hossain Avatar answered Aug 06 '26 14:08

Anowar Hossain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!