Is it possible to change page urls in codeigniter - codeigniter

Is it possible to change the collections page urls from https://www.product.com/collections/view/14e92dd34fb680e94c02e9ebd2ce36b29e92fd8a-*4*75 this like below http://www.product.com/larimar-jewelry/abril-necklace we do not want any random text in the urls. in codeigniter

You can set your alias in application/config/routes.php
$route['alias-value'] = 'controller/method';
refer documentation here
Otherwise you can do this with rewrite rule using .htaccess file.
Please refer doc

Related

Set a custom url for Codeigniter Controller

Please can any one suggest how to shorten the url
http://localhost:8080/MyWebApp/index.php/Cpanel_control/
to
http://localhost:8080/MyWebApp/Cpanel
in Codeigniter using routes.
I tried it in this way
$route['Cpanel'] = "MyWebApp/index/Cpanel_control";
But did not work
To remove index.php from your url in CI, you need .htaccess file.
Check this out https://gist.github.com/philipptempel/4226750
I'm assuming Cpanel_control is a valid controller.
For the routing, you can have this in your routes settings
$route['Cpanel'] = "Cpanel_control";
To avoid any other issues, make sure base_url in config file is set thus
$config['base_url'] = "http://localhost:8080/MyWebApp";
New Approach
Routing:
In some instances, however, you may want to remap this relationship so
that a different class/method can be called instead of the one
corresponding to the URL.
A short excerpt from the CI doc shows that you can't use routing here. Instead you should go for a mod_rewrite rule (.htaccess)
RewriteEngine On
RewriteRule ^index/Cpanel_control/(.*) Cpanel/$1 [R]
Old approach
According to the corresponding documentation, the paths are both not absolute. Furthermore, you need to set the "from"-URI as the array key and the "to" URI as the string. If you want to route index/Cpanel_control to Cpanel, you need to swap the URIs of you example.
So this would be correct:
$route['index/Cpanel_control'] = "Cpanel";

Codeigniter URL routing solution for my website

I have a website that is developed with CodeIgniter. I have added the route for my url as follows:
$route['about_us'] = 'about-us';
Now I have a problem with that. I.e. when I am looking for the url www.mysite.com/about_us it works and at same time www.mysite.com/about-us is also working. I want only one url to work: the one with the underscore.
I have removed this to:
$route['about_us'] = 'about-us';
But the url www.mysite.com/about-us still works. It may cause duplicate content for my website in Google and so more page links also showing. Even I don't have that functions too. Like www.mysite.com/about_us/design. Likewise in about_us controller file index function only there, but design method calling in Google.
How do I resolve this problem?
You actually don't need a route here. The normal purpose of request routing the way you are using it is so that you can use hyphenated URLs when hyphens are not permitted in class and function names. I.E. you want the url to by www.example.com/test-controller, but you can't actually name a controller test-controller because the hyphen is illegal.
If you only want to have the underscored URL such as www.mysite.com/about_us then just remove the route completely and name the controller about_us. With no routing rules the hyphenated url should 404.

Can CodeIgniter route custom URLs?

I am trying to get pretty URLs on Codeigniter. Basically, here’s what I want.
//http://website.com/State/City/Neighborhood/Zip => http://website.com/search?lotsofParams=1234
$route['/State/City/Neighborhood/Zip'] = "search?location=blahblahblah&Submit=Search";
I am willing to write the routes manually, but right now that code isn’t working. Any idea how I can get this working?
no you are using QUERY STRINGS you can't route query string to friendly slashed urls.
You can re-write them with htaccess

How can I shorten routes in Codeigniter for certain requests?

I have a page that has this category URL website.com/category/view/honda-red-car and I just want it to say http://website.com/honda-red-car no html or php and get rid of the category view in the URL.. this website has been done using the CodeIgniter framework..
also this product view URL website.com/product/details/13/honda-accord-red-car
and I want it to be website.com/honda-accord-red-car PLEASE HELP!!!
I cannot find correct instructions on what I am doing wrong??
In Routes.php you need to create one like so
$route['mycar'] = "controller_name/function_name";
So for your example it would be:
$route['honda-red-car] = "category/view/honda-red-car";
Take a look into the URI Routing part of the user guide.
If you have concrete set of urls that you want to route then by adding rules to the application/config/routes.php you should be able to achieve what you want.
If you want some general solution (any uri segment can be a product/details page) then you might need to add every other url explicitly to the routes.php config file and set up a catch-all rule to route everything else to the right controller/method. Remember to handle 404 urls too!
Examples:
Lets say the /honda-red-car is something special and you want only this one to be redirected internally you write:
$routes['honda-red-car'] = 'product/details/13/honda-accord-red-car';
If you want to generalize everything that starts with the honda- string you do:
$routes['(honda-.*)'] = 'product/details_by_slug/$1'; // imaginary endpoint
These rules are used inside a preg_replace() call passing in the key as the pattern, and the value as the replace string, so the () are for capture groups, $1 for placing the capture part.
Be careful with the patterns, if they are too general they might catch every request coming in, so:
$routes['(.*)'] = 'product/details_by_slug/$1';
While it would certainly work for any car name like suzuki-swift-car too it would catch the ordinary root url, or the product/details/42 request too.
These rules are evaulated top to bottom, so start with specific rules at the top and leave general rules at the end of the file.

How to convert _ to - in urls for codeigniter?

Been trying to learn codeigniter, 1 problem I had though is that if I had a function say
top_10()
It would mean that my urls will be something like
..../top_10/
Which is fine but I prefer - more than _ for urls. I tried changing my function names to top-10(), but it results in a syntax error it seems (even if it doesn't it results in a ugly function name), Is there a way to let codeigniter auto converts all the _ in my controller functions to - when it comes to the urls?
.../top_10/ -> .../top-10/ for all other similar controller functions.
You can use the URI routing rule
something like :
$route['controller/top-10'] = "controller/top_10";
Add this line in the routes.php file inside application/config/routes.php
Here controller/top-10 will be the part of your url which will be routed to the desired function provided by on the right hand side.
also if you aren't using mod_rewrite to rewrite your url then your URL will appear some thing like
http://localhost/index.php/controller/top-10
Inside your application/config/routes.php folder if there isn't a line for translating uri dashes then add this line: $route['translate_uri_dashes'] = TRUE;
If $route['translate_uri_dashes'] already exists, simply set it's value to true.

Resources