0

I'm working on a project where I need to create clean URL for every products, see following pattern:-

example.com/Single_Product_Page

The original URL is example.com/browse/Single_Product_Page and I used following code:-

$route['(:any)'] = 'Products/browse/$1'; 

I've two page (1) example.com/Product
and (2) example.com/Products/Single_Product_Page  

 

And it is working fine but now I'm unable to open Products page and when I tried to open it, It open Single_Product_Page

Please help me to solve this issue.

2 Answers 2

2

You need to update your routes similar to this example (which works OK on my site):

$route['products'] = 'controller/myfunction/argument';
$route['products/:any'] = 'controller/myfunction/another_argument/$1/1';

you can get more insight from the docs here

Sign up to request clarification or add additional context in comments.

2 Comments

I don't want URL like example.com/Products/Single_Product_Page but I need clean URL like example.com/Single_Product_Page.
this is not quite standard and you will loose SEO friendliness, if all your products show in the single_product_page (instead of "single_product_page/redshirt" and "single_product_page/greenshirt"; anyway you could achieve this with loading ajax content into that page
1

You can use a little hack to use just your controller name (and that's a must of course) but eliminate the need to write the method name and pass your parameters directly after the controller name so basically your url would look like this: http://localhost/controller/parameter and that would give you shorter urls as you intend but not an SEO friendly as you claimed.

You can use _remap in your controller and check if it matched a method process it normally or pass it to your index (which is your default method that's doesn't have to be written in your url) .. and now you won't need to use index in your url as you intended.

public function _remap($method)
{
    if ($method === 'some_method_in_your_controller')
    {
            $this->$method();
    }
    else
    {
            $this->index($method);
    }
}

Or you can depend on ajax for all of your crud operations and your url will be pretty much fixed all the time.

Comments

Your Answer

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