I want to create more SEO friendly URL for my products which are stored in the database.
How can I get ProductName from the URL?
For example:
http://domain.com/ProductName
Where ProductName is not Controller. I need to validate the ProductName from the controller and display it.
Personally I would create a controller called products that accepts a parameter to find a product from the database:
class Products extends CI_Controller
{
public function index($productname)
{
$data['product'] = $this->db->get_where('products', array('urlname' => $product))->row_array();
// other functionality here and view loading
}
}
Then add routing for this scenario (config/routes.php):
$route['other/routes/first'] = "their/correct/controller/$1";
$route['/(:any)'] = "/products/index/$1";
It's been a while since I used CI but this should help point you in the right direction I hope.
You can add a routing rule in routes.php
Redirect the request to Product controller
$route['(:any)'] = "product/$1";
In your Product controller, get the product name from URI segment
$product_name = $this->uri->segment(2);
You can use the URI Routing (Routing Rules).
Routing rules are defined in your application/config/routes.php file. In it you'll see an array called $route that permits you to specify your own routing criteria. Routes can either be specified using wildcards or Regular Expressions
$route['/(:any)'] = "/products/index/$1";
this example would result in: http://www.example.com/MyProductName/ loading the products class giving the name as parameter.
You can make use of CI routes to achieve this
Edit Application/config/routes.php
$route['/(:any)'] = "/products/index/$1";
Related
I'm building an online store based on CodeIgniter. I'd like URLs to look like this? What is the solution for this type of SEO friendly url.
http://example.com/[product-category]/[product-sub category]
I need this url:
example.com/women/sarees-sari
But my url is generated
example.com/Product/item/MQ==/women/sarees-sari
/Product/ is my controller,
/item/ is function name,
/MQ==/ is my product id
You can use routing to handle your request url. It's simple. For example for your case:
$route['women/sarees-sari'] = 'Product/item/MQ==';
Codeigniter has _remap function that can be called on controllers. So you can call this on core controller or main controller, and call your function that wish.
CodeIgniter has very good routing System so you can modify your url as per your requirement and linking using /application/config/routes.php file.
If you open this file first time, you will see only default controller, i.e $route['default_controller'] = 'welcome';
but you can add as many routes as you want. Like in your case for seo, you should add
$route['women-sarees-sari'] = 'Product/item/MQ=='; and this will route the user from www.example.com/women-sarees-sari to correct controller and method.
i am currently coding in codeigniter and i want to remove the controller and function name from url so that only product slug is displayed in url.
currently my url is like
www.example.com/main/singlePage/41/product-slug
I want the url to be like
www.example.com/product-slug
To do this create a "catch all" route $route['{any}'] = 'controller/function'
and in this function of the controller get the slug $slug = $this->uri->uri_string() and decide what to do based on it.
We can use routes for that, In your application/config/routes.php.
See the example,
$route['{any}'] = 'pages/slug_on_the_fly'
Ref: https://codeigniter.com/user_guide/general/routing.html
I am creating e-commerce website in CodeIgniter and I Have to prepare SEO friendly URL.
Currently My URL is:-
abc.com/product/product_list/1/categoryName
product is my controller
product_list is my function name
1 = category id
categoryName is my category Name
And I have to make like this:-
abc.com/categoryName
I have to remove controller name as well as function name and one uri segment i.e., category id and show only category name.
How can I do this? Is this possible using .htaccess file?
You can use route as:
$route["(:any)"] = "product/product_list/$1";
But issue is that it will hurt your other controllers like if you have page like
abc.com/contactus
It will call the product/product_list/. I suggest you to use route as:
$route["category/(:any)"] = "product/product_list/$1";
You can use URL like:
abc.com/category/yourCatName
And get category name in controller by using CI Segments.
CI Routes
I want to dynamic function name of controller in codeigniter like:
$functionName = 'profile';
function $functionName(){
------
-----
-----
}
Actually I want to change url http://website/manage/viewprfile/Username to http://website/manage/profile/Username and that url is used in whole website.
jus add this routes in application/config/routes.php
$route['manage'] = 'manage/index';
$route['manage/(:any)'] = 'manage/$1';
now u can access to manage/profile/username, manage/somethingelse
the simple way is to write route rules in following way$route['manage/prfile '] = 'manage/viewprfile';
this will change your URL to specific that you want.
Other way is to create a hook for rewriting your URL.
Problem is:
Controller: products
Method: product_details(category_name, product_name)
Current URL is: http://www.domain.com/products/product_details/category-name/product-name
But i don't want the above url, I want to enter below URL and its remapped on the above mention method
URL TO MAPPED: http://www.domain.com/product/category-name/product-name
How to do it using routes.php in codeigniter?
This should do the trick:
$route['products/(:any)/(:any)'] = 'products/product_details/$1/$2';
If your products controller has any other methods you will need to specify routes for them above this, as this is essentially a catch all route for your products controller.