6

I want to have a clean URL in CodeIgniter based application for User's Profile Information.

Please Take a look at URL formats below.

Actual URL : http://www.mydomain.com/index.php/users/profile/user1

I'm expecting users to have Personal URL's like

http://www.mydomain.com/user1
http://www.mydomain.com/user2
http://www.mydomain.com/user3

URL http://www.mydomain.com/user1 should process http://www.mydomain.com/index.php/users/profile/user1 in background execution.

I will be removing index.php from URL using Route library.

Thanks in advance for any sort of help.

Andreas Bergström
  • 13,891
  • 5
  • 59
  • 53
pinaldesai
  • 1,835
  • 2
  • 13
  • 22

2 Answers2

10

Have a look at https://www.codeigniter.com/user_guide/general/routing.html.

$route['user(:num)'] = "users/profile/user/$1";

If you mean you want /anyusername to route to the users controller, you would have to put:

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

At the bottom of routes.php and every non user-URL above it. Otherwise every URL would be routed there, obviously. You will need to implement some mechanism in the users-controller to throw 404-errors, since you are routing all requests not catched in the routing rules above.

Stack Programmer
  • 679
  • 6
  • 18
Andreas Bergström
  • 13,891
  • 5
  • 59
  • 53
  • 3
    Somehow, I have a feeling the users' name can be any arbitrary combination of letters and numbers. (i.e. not `user[0-9]`) – Kemal Fadillah Oct 09 '12 at 06:11
  • Your answer is what i have expected, now only one concern here which is - how actual URL (Non userprofile URL) will work or taken care of ? url can be http://www.mydomain.com/contact. Do you mean I have to write down and route each possible URL to overcome this situation ? – pinaldesai Oct 09 '12 at 06:35
  • You don't have to route every specific URL, but every / section you have. Such as /contact/:any, /start/:any, etc. – Andreas Bergström Oct 09 '12 at 06:38
1

IN config/routes.php

add this line

$route['user(:num)'] = "users/profile/$1";
Pramod Kumar Sharma
  • 7,851
  • 5
  • 28
  • 53