0

I have pages that are generated from a database, based on the URI. It functions as it should, however I can't set-up my routes to eliminate the controller and function from the URL.

$route['studios/(:any)'] = 'studios/location/$1';

Right now, I have the route to show the controller name and the URI variable (whatever that may be). However, I want to eliminate the controller name as well and just display the URI variable that's called as the URL.

Current URL would be: domain.com/studios/studio1

But I want to just display: domain.com/studio1

I tried $route['/(:any)'] = 'studios/location/$1';, but that's messing up my entire site.

What can I try next?

2 Answers 2

1
$route['studios(/:any)*'] = 'studios/location';

This route will force everything from studios on to studios/location. You can then access any of the parameters using URI segments:

$id = $this->uri->segment(2);

If your URL was somewhere.com/studios/location/2, $id would resolve to 2

However, since you want it to just be from the root on, you will have to put your override route at the bottom of the routes file so it is assessed last:

// all other routes here. Which must be specifically 
// defined if you want a catch all like the one you mentioned
$route['(:any)'] = 'studios/location';

Alternatively, if you want a high maintenance site, you can specify a collection of routes like so:

$route['(studio1|studio2|studio3)'] = 'studios/location/$1';
Sign up to request clarification or add additional context in comments.

Comments

0

how is it "messing up your site"?

In any case, you should not have the / before (:any)

Just:

$route['(:any)'] = 'studios/location/$1';

EDIT:

BEFORE the $route['(:any)'], you'll need to specify routes fro all your controllers; this is pretty normal, don't know if I'd call it "high maintenance", but you'll need to decide

3 Comments

probably cause that route forces every request to studios/location
that's what OP says he wants; ah, I see what you mean. That explains "messes up my entire site". I'll edit above
Yeah, the only reason it would be considered high maintenance is because by default CI will access by controller/method/parameters and when there's a catch all it pretty much hijacks that. You'd need to specify a route for all pages above the catch all and if you for some reason have an ever changing site or hand it off to someone else they have to know about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.