Welcome Guest, Not a member yet? Register   Sign In
Help and suggestions for making a short URL structure?

Hi all,

I am building a new shop site and I had a plan for the url's but cannot see how to do it efficiently.

Normally I would have urls something like this:

Where of course the product controller (index method) would decode the product name and show the relevant product. Same for ranges and sectors.

But I would like this time to do this:

But as far as I can tell, the only way I can do this is to have a controller for each product, sector or range, which is of course impossible (or at least inefficient).

I was wondering if there was something I could do with routes, but again, there is no way I can think of that will also work with the other 'normal' urls like


I also thought about customising the CI routing so that in the case of the controller 'large-ceramic-teapot' not being found, routing to a default controller that could deal with if the request is for a product, range or sector or not. However, tinkering with the CI routing is not something I am keen on trying. I am sure I will just bugger something up without realising I have until later in the project.

Does anyone have any suggestions about how I could achieve this shorter url?

I suspect the answer is that there is no way to do it, and I will have to go back to the original longer urls.

Thank you in advance for any suggestions,

Best wishes,


There is a way of doing that, but it is a quite long topic for writing it in a forum. If you are interested, write me a private message, I'll give you my CMS for free.

That is very kind of you, thank you.

I would love to have a look at it, messaging you now.

Thank you again,


(This post was last modified: 12-02-2017, 01:29 PM by PaulD. Edit Reason: Added PS )

I was thinking about a pre-system hook to change the routing. So I could look up the first segment, and somehow reset the controller called if necessary (not exactly certain how yet, but been looking through codeigniter.php and index.php for ideas).

So armed with this new idea I found this: https://stackoverflow.com/questions/2885...on-best-ap

That suggests something like the following for a pre-system hook. (just tidied up the comments a bit, not changed anything else yet)
function set_controller()
    // Gather the DB connection settings
    include_once APPPATH.'config/database.php';
    // Connect to the DB server
    $link = mysql_connect($db[$active_group]['hostname'], $db[$active_group]['username'], $db[$active_group]['password']) or die('Could not connect to server.' );
    // Select the DB
    mysql_select_db($db[$active_group]['database'], $link) or die('Could not select database.');
    // Break apart the URL variable
    $URI = explode('/',key($_GET));
    // Query the DB with the URI segment
    $query = 'SELECT * FROM theDomainTable WHERE domainName = "'.$URI[1].'"';
    // Only deal with controller requests that exist in the database
    if($results = mysql_fetch_array(mysql_query($query))){
        // Replace the controller segment
        $URI[1] = $results['controllerName'];
        // Reconstruct and replace the GET variable
        $_GET = array(implode('/',$URI)=>NULL);
    // Close the DB link

That looks like it has potential as a way to go.

I also had a look at the routing where it throws an error if the controller is not found. I was tempted to do the lookups there and tamper with it but in truth, there are things it is doing in the php there I do not fully understand. (And tampering with these files is always a bad idea)

I might give the above code a go, see if I can modify it to do what I need. Still causes me concern though as this is rapidly getting beyond my working knowledge of CI, and I do not want to cause massive issues further down the line with unforeseen side affects.


PS This doesn't seem to be working. At least I have been unable to get this idea to work :-(

You could point all specific routes to specific controllers and all others to the Products controller:

PHP Code:
// Not tested
$route['members'] = 'members';
$route['checkout'] = 'checkout';
$route['(:any)'] = 'products/index'

Maybe be you also want to get Routes from a Database field.

(12-02-2017, 12:21 PM)natanfelles Wrote: Maybe be you also want to get Routes from a Database field.

Hey thanks natanfelles. That looks like a possibility. The link you gave provides a neat database driven routes solution. That might work although I do worry about how many routes is too many? (Before negative side affects start).

The idea of your routes doing all the normal urls and then the 'any' solution might work too.

The previous idea of the pre-system controller was a no-go. Just can't seem to get it to work.

Thank you for the suggestion. Will try it tonight or perhaps tomorrow (I think I have had enough of this now - was about to give up and just go with normal urls before your post).

Thanks again,


Theme © iAndrew 2016 - Forum software by © MyBB