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
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 beginner on Codeigniter so I need help.
I am making blog site with Codeigniterand I don't know how to hide controller class, and ID from URL.
Like I have generated following URL: http://localhost/codeigniter/index.php/content/some-title/123.
content is my controller class name.
some-title is my article title.
123 is my article id.
I want to keep only title to show.
For example: http://localhost/codeigniter/index.php/some-title.
Any help would be appreciated!!!
You has add your link config/routes.php
http://localhost/codeigniter/index.php/content/some-title/123
Ex: $route ['some-title/(: any)'] = 'content/some-title/$123';
You can do this:
$route['(:any)'] = 'content/view/$1';
and in your content controller you must have a method like the one follow:
public funtion view($pagetitle){…}
In the routes.php make sure you are using this line after your other routes which starts with some text.
$route['(:any)'] = 'content/show_article_post/$1';
Also make sure your slug for each article is unique in your database. There is no other way to send hidden id from get request.
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.
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";