I have created a multi-language website.
Facing issue while creating SEO user-friendly URL.
current URL:
http://localhost/example/en/user/myaccount OR
http://localhost/example/fr/user/myaccount
want to change it with
http://localhost/example/en/myaccount OR
http://localhost/example/fr/user/myaccount
routes.php
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// URI like '/en/about' -> use controller 'about'
$route['^(en|de|fr|nl|id|es)/(.+)$'] = "$2";
// '/en', '/de', '/fr','/es' and '/nl' URIs -> use default controller
$route['^(en|de|fr|nl|id|es)$'] = $route['default_controller'];
also tried with
$route["myaccount"] = "user/myaccount";
$route["^(en|de|fr|nl|id|es)$/myaccount"] = "user/myaccount";
Nothing will work in this case. Already used following in routes.php file other demo projects with out multi-language. There it's work perfectly.
$route["myaccount"] = "user/myaccount";
Thanks in advance
you can consult the documentation for routes here
https://www.codeigniter.com/user_guide/general/routing.html
and for your problem
$route["(:any)/myaccount"] = "user/myaccount/$1";
Related
How can set auto route 404 page of frontend and backend different ? I have searched a long time but i have not found perfect answer.
Btw, can we make the same for error 500 ?
You can easily do it by using conditional statement.
go to application/config/routes.php and remove:
$route['404_override'] = '';
After that add the following code.
$req_uri = $_SERVER['REQUEST_URI']; // $req_uri = /myproject/backend
$req_uri = explode('/', $req_uri);
$req_uri = $req_uri[2]; // $req_uri[2] = backend
if($req_uri == 'backend'){
$route['404_override'] = 'Backend_error'; // Not found controller for backend
}else {
$route['404_override'] = 'Frontend_error'; // Not found controller for frontend
}
You can use echo statement to analyze further. and then do more stuff accordingly.
I've configured Codeigniter 2.1 with i18n and extended controller.
I've hide the main controller "main" and I kept visible "admin" and "blog" controller.
This urls works fine:
www.mysite.com/ en / functionname
www.mysite.com/ en / blog /
This is my problem:
www.mysite.com/ it / blog / functionname
With the main controller "blog" everything after "/" is ignored.
Is it possible to do this?
My routes.php file:
$default_controller = "main";
$language_alias = array('it','en');
// exceptions
$controller_exceptions = array('admin','blog');
// route
$route['default_controller'] = $default_controller;
$route["^(".implode('|', $language_alias).")/(".implode('|', $controller_exceptions).")(.*)"] = '$2';
$route["^(".implode('|', $language_alias).")?/(.*)"] = $default_controller.'/$2';
$route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/$1';
foreach($language_alias as $language) {
$route[$language] = $default_controller.'/index';
}
$route['404_override'] = '';
// URI like '/en/about' -> use controller 'about'
$route['^(it|en)/(.+)$'] = "$2";
// '/it', '/en' URIs -> use default controller
$route['^(it|en)$'] = $route['default_controller'];
If I remove lang in my URL everything works fine:
www.mysite.com/ blog / functionname
I think you need another segment in your routes for accessing the controllers function.
so you'll need a second line:
// '/it', '/en' URIs -> use default controller
$route['^(it|en)$'] = $route['default_controller'];
// URI like '/en/about' -> use controller 'about'
$route['^(it|en)/(.+)$'] = "$2";
// URI like '/en/about/test' -> use controller 'about' with function 'test'
$route['^(it|en)/(.+)/(.+)$'] = "$2/$3";
I don't use the i18n or support multiple language in any project atm so I can't test but this should do the trick.
I am developing a single page web site so all routes should lead to the default router. This is how I've achieved that through the routes.php file:
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['admin'] = 'admin/home';
$route['^/'] = "home";
$route['(.*)'] = $route['default_controller'];
Thats working as expected on a test server and on the local host, but on the actuall server, when I try to find any of the pages I am getting the 404 error. Any ideas what could be wrong?
$route['default_controller'] = "home";
$route['404_override'] = "home/index";
$route['.*'] = "home/index/$1"
I have the this route defined in routes.php
$route['user'] = "user_controller";. The user controller has a method logout() but when I try this URI user/logout I get a 404. In the same way when I use this URI user/index I get a 404.
routes.php
// custom routes
$route['start'] = "start_controller";
$route['register'] = "register_controller";
$route['user'] = "user_controller";
// other routes
$route['default_controller'] = "start_controller";
$route['404_override'] = '';
According to CI
Note: Routes will run in the order they are defined. Higher routes
will always take precedence over lower ones.
$route['default_controller'] and $route['404_override'] must always be on top above others
$route['user/logout'] = "user_controller/logout";
$route['user/index'] = "user_controller";
Example i will type a user/logout then it will proceed to user_controller/logout you have to define the URL you would like to redirect
Yep, you have to specify a route for each particular method.
Here's an example from my routes.php :
/* User Authentication Controller */
$route['login'] = "auth/login";
$route['logout'] = "auth/logout";
$route['register'] = "auth/register";
$route['forgot'] = "auth/forgot";
When using
$route['(:any)'] = 'pages/view/$1';
and I want to use other controllers in my routing for example:
$route['del/(:any)'] = 'crud/del';
it won't work. I guess it will use
pages/view/del/$1
and not my crud-controller when deleting an item. How can I solve this?
As indicated, $route['(:any)'] will match any URL, so place your other custom routes before the "catch-all" route:
$route['del/(:any)'] = 'crud/del';
// Other routes as needed...
$route['(:any)'] = 'pages/view/$1';
Its hundred percent working
$route['(:any)'] url is placed last in your routes file
$route['(:any)/company_product_deal_detail'] = "mypage_product_picture/deal_detail/$1";
$route['(:any)/company_service_deals/(:any)'] = "mypage_service_deal_list/index/$1";
$route['(:any)/company_service_deals'] = "mypage_service_deal_list/index/$1";
$route['(:any)'] = "company/index/$1";
I know that it's an old question, but I have found myself a nice solution.
By default, CodeIgniter gives priority to URL's from routes config (even if straight controller, method etc. specified), so I have reversed this priority this way:
In system/core/Router.php find _parse_routes method.
Add this code under literal route match:
$cont_segments = $this->_validate_request($this->uri->segments);
if ($cont_segments == $this->uri->segments) {
return $this->_set_request($cont_segments);
}
I agree, that this approach is kinda wrong, because we edit file from system/core, but I needed a fast soluttion to work with a lot of URL's.