Crab
Crab

Reputation: 21

Additional URI segment before Codeigniter controller

I'm making this little project in Codeigniter and I need to have one additional segment before controller.
The default Codeigniter URL is:

base_url/controller/function/parameter1/../parametern  

What I need is:

base_url/something/controller/function/parameter1/../parametern 

Where "something" is a name that user creates so I cant create a subfolder in controllers folder as suggested in other topics because its dynamic and each user chooses what he likes.

Basically what I need is that it like ignores that segment (I would catch that segment in a hook, extend CI_controller or something like that and validate it), for example if I write base_url/stackoverflow-rocks/home/function, base_url/asd-whatever/home/function Codeigniter would look at it like base_url/home/function

I have also looked at Passing variables before controller URI segment in CodeIgniter question, its almost the same as mine but the suggested answer didn't work.
I used $route['(:any)/(:any)'] = '$2'; which works if the url is base_url/whatever/home, but if the url is base_url/whatever/home/function or base_url/whatever/home/function/param1/../paramn it doesn't, the workaround for this is to write:

$route['(:any)/(:any)'] = '$2';  
$route['(:any)/(:any)/(:any)'] = '$2/$3';  
...  
$route['(:any)/../(:any)'] = '$2/../$n'  

which is easy, but seems a bit lame, and it will only work if I have from 2 to n segments in url. I also tried to mix it with regular expressions, like:

$route['(:any)/(.*)'] = '$2'

But it works just like the previous one, (.*) is just one segment but not the whole url... Is there some way to just write
$route['(:any)/rest of the url'] = 'rest of the url';?
I also tried using .htaccess:

RewriteRule ([^/]*)/(.*) inedx.php?/$2  

or

RewriteRule (.*)/(.*) index.php?/$2

But it didn't work because $2 referenced to the first part, for example if I write:
base_url/one/two it rewrites it to base_url/index.php?/one. But if I use:

RewriteRule (.*)/(.*) index.php?/$1/test/$2

it references as it should, for example base_url/one/two => base_url/index.php?/one/test/two, so $1 = one and $2 = two, as it should be.

Quite a long question but I hope someone can help, thanks!

Upvotes: 2

Views: 1298

Answers (1)

SNAG
SNAG

Reputation: 2113

see if this works:

$route['base_url/controller/(:any)'] = 'base_url/$1'

Upvotes: 0

Related Questions