conditional routing (shorten url) |
Hi, is it possible to have conditional in routes.php like below
Code: $req = $this->uri->segment(1); I want to shorten the url from www.example.com/locations/sydney to www.example.com/sydney but the code above somehow don't work, it only applies to the first if statement. Any suggestion?? My Home controller is looking like below, I have Locations, Towing_services, Cash_for_cars, and Blog controllers look exactly the like below Code: class Home extends CI_Controller Can anyone help me please. Thanks
@acebay,
Nothing in the current CI documentation about routing can help you ( https://www.codeigniter.com/userguide3/g...ri-routing )?
Because it's config file, it doesn't have access to database.
While it might feel a bit "hacky", you could set up 404 controller, and if route fails, you could still catch it and deal with it, if it's a short hand for existing page.
Can I shorten my url with .htaccess? Is it possible in my case.
Say I have these urls in my website http://www.example.com/locations http://www.example.com/locations/sydney http://www.example.com/locations/brisbane http://www.example.com/locations/perth http://www.example.com/services/ http://www.example.com/services/cars http://www.example.com/services/truck http://www.example.com/services/phone http://www.example.com/blog http://www.example.com/blog/display/blog-title http://www.example.com/blog/display/blog-title2 http://www.example.com/blog/display/blog-title3 http://www.example.com/blog/display/blog-title4 what I want is the urls to be like these http://www.example.com/locations http://www.example.com/sydney http://www.example.com/brisbane http://www.example.com/perth http://www.example.com/services/ http://www.example.com/cars http://www.example.com/truck http://www.example.com/phone http://www.example.com/blog http://www.example.com/blog-title http://www.example.com/blog-title2 http://www.example.com/blog-title3 http://www.example.com/blog-title4 how to achieve this in codeigniter?? (01-27-2019, 12:49 PM)Pertti Wrote: Because it's config file, it doesn't have access to database. Can you please explain more how to hack it with 404 controler? Sorry I'm just new to programming, need some help. Thanks (01-28-2019, 09:49 AM)php_rocs Wrote: @acebay, Hi, thanks for the tip, I've read the documents, but there's nothing there that can help shortening the url for my case. Thanks
Here's one potential way to handle it, though it comes with a pretty big caveat:
- In your routes config file do a "catch-all" route as the very last route. That way any route not found or specified above that is sent to the controller/method of your choice. - In that controller you'd have to parse the URL segments manually to determine what page to show based on your app's business logic. The big caveat is that this means you MUST manually specify all other routes in the system, instead of relying on CI's automatic routing based on Controller/Method name in the URL. This same type of setup could be used for the 404 Controller as Pertti said. Check the Reserved Routes section on the URI Routing page to see how to override the 404 action. (01-28-2019, 02:58 PM)kilishan Wrote: Here's one potential way to handle it, though it comes with a pretty big caveat: Hi, thank you for the tips, yeah I did that, I specify all the pages in my routes.php so it will have the URL that I intend (code below). ////////////////////////////////// $route['about-us'] = 'home/display/about-us'; $route['blog'] = 'home/display/blog'; $route['testimonials'] = 'home/display/testimonials'; $route['car-towing'] = 'towing-services/display/car-towing'; $route['truck-towing'] = 'towing-services/display/truck-towing'; $route['motorbike-towing'] = 'towing-services/display/motorbike-towing'; ///// There's more, but you got the point ////////// But the only problem is that I created a backend that can create a new page, so every time I create a new page I have to write the page-url in routes.php, it's not automatic ![]() If anyone wondering why I'm using flat structure URL instead Silo structure URL, it's for SEO purposes. |
Welcome Guest, Not a member yet? Register Sign In |