CodeIgniter Forums
custom users routes? [SOLVED] - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: custom users routes? [SOLVED] (/showthread.php?tid=34937)

custom users routes? [SOLVED] - El Forum - 10-14-2010

Hi guys,

I developed over 30+ sites in CI, and I love it! Smile
Now, I have a specific request, which is giving me routes-problem.

There is this site, let's call it:

ok, now, every USER on that site, should have a custom URL, for *all of his pages*. Here's an example:

ok, so - this would be easy, if it was a "single page for each user", so I could easily rewrite /config/routes.php to interpret something like this: -> ->

ok, that would be easy. BUT, what I really need to do, is to make their username *part of their URL*, so that they have other pages, like they have multiple CI instances, but it's only a URL trick. So, I need to be able to route these pages as well:

get it? Smile

So, basically, what I need to do is: to have only one instance of my CI, and to find a way to put username in the URL, so that it acts like it's a base_url(), but the real application code is in the root of the "", of course.

Is that doable? :|

I know that I can add suffix on every URL, transfering their username that way, for example:

but I don't want that, I need to make the URLs very user-friendly so that they can share their "mini websites" with short and simple URLs.

Any way for this to be done?
Thanks for your time and effort upfront guys, I really appreciate it. Smile

Cheers from Europe,

p.s. perhaps something can be done similar to the "multilanguage URL hack" like in the CI Wiki? ?? Something like that?

custom users routes? [SOLVED] - El Forum - 10-14-2010

if using apache should be possible to do a url rewrite to change from

although actually you probably need to change the url to something like this

custom users routes? [SOLVED] - El Forum - 10-14-2010

You need something that is uniform across the board.


then you can at least use something like:

$route['^users/([^/]+)/(.*)$'] = '$2/$1';
// this is untested and quickly off the top of my head. For demonstration only.

Or you can use wildcard subdomains:

custom users routes? [SOLVED] - El Forum - 10-15-2010

Thanks for your help guys, but I don't think that will do the trick. Smile

It amazes me how something this obviously needed is so hard for me (us?) to figure out? Smile
I can almost bet that there is a way to squeeze one prefixed parameter in URL, like with the language Lib, and that's what I must focus on. That, or one copy of .htaccess in default folder for each user? Would that work??
something like:

and the real CI code would be here, of course:

hmmm.. I wonder if this could do the trick.
I'll test it over the weekend, and hit you back if I get any good results.

Thanks again! Smile

custom users routes? [SOLVED] - El Forum - 10-15-2010

Quote:It amazes me how something this obviously needed is so hard for me (us?) to figure out?

No offense, but I didn't put much thought into my suggestion other than stating the obvious. It's your project and, thus, everyone here is expecting you to put the majority of the thought into your problem. Smile

I have looked at the language lib you referenced yesterday. It has a custom routing rule that looks for a two-letter prefixed parameter. Unless you can be assured that all your users will have two-letter usernames, you might need some similar uniformity in your URL schema.

Personally, I'd just use subdomains and abstract all this routing business out of the problem. Smile

Good luck!

custom users routes? [SOLVED] - El Forum - 10-15-2010

Create a hook or a library extension that extracts the first segment from the URI, and checks if it's a valid username.

If so, store it somewhere (a config variable perhaps) where it can be retrieved by your controllers. Then strip the segment from the URI, so that routing happens on the next segments.

If not, show an error message, or redirect to a generic page.

custom users routes? [SOLVED] - El Forum - 10-16-2010

no offense taken my friend Smile

however, I found a way (hack) how to achieve this.

I edited file /libraries/Router.php, and after this line:

// Fetch the complete URI string

// ----------------------
$store_name_end_pos = strpos($this->uri->uri_string, "/", 1);
$store_name = substr($this->uri->uri_string, 0, $store_name_end_pos);
$store_name = str_replace('/', '', $store_name); // remove all "/"
$this->config->set_item('store_name', $store_name); // so that I could use this later in the code
$this->uri->uri_string = substr($this->uri->uri_string, $store_name_end_pos); // remove this /$store_name/ from CI routes logic


I created /libraries/MY_Router.php
so that I could track this change easily in the future, but that's bonus Smile

so far - it's working. Smile

thanks for your help, time, and effort guys, all of you, really. Smile
I hope this will help more users in time.


custom users routes? [SOLVED] - El Forum - 10-16-2010

Nice hack Dustin. I love this about codeigniter. It's fairly easy to modify.

Just wondering, will all your URL's have a username? I like WanWizards approach of checking for a valid username. I know that's a lot of overhead, but you could cache usernames in memory (memcache or APC, etc.) and bypass your routing code when the username is invalid (and thus use URI paths where a username is not required.) If you actually need to do this, you might want to make sure that your users can never register a username that you ever anticipate using in your URL's.

Glad you figured it (WanWizard gave much better advice, as usual.)

custom users routes? [SOLVED] - El Forum - 08-03-2011

i added some custom_routing in config/routes

$route['(:any).?([^exclude_controller])'] = 'users/controller_to_view_user_page';

Catch the additional parameter on URI, to give the right user pages.
I don't know, is this a good practice or not. buat somehow it works.

It's amaze me how i could do this regex thing.. im totally noobs

custom users routes? [SOLVED] - El Forum - 08-04-2011

If I understand you correctly then I think you can use the standard config/routes.php

// Set your default path
  $route["default_controller"]      = "welcome_page";

// test for specific controllers
$x=array("(:any)",'about', 'contact_us','terms','blog','subscribe','specific_controler');
  foreach($x as $xx):
   $route[$xx] = "$1";

// fall-through to your login section
// finally the Daddy of them all
  $route["(:any) = "login/$1"; // extract using $this->uri->uri_string() or segment(...)