CodeIgniter router doesn't work on a specific server - codeigniter

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"

Related

Make user-friendly URL in Codeigniter with multi-language support

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";

load multiple Domains on a single website

I have multiple domains
mysite.com - Main website
other domains
test.com
book.com
aaa.com
...
I have a website based on Codeigniter
I want to load other domains on this address
test.com (mysite.com/seller/test)
book.com ( mysite.com/seller/book)
aaa.com (mysite.com/seller/aaa)
I have these pages on seller websites
test.com/about
test.com/contact
test.com/shop
it's load this page
mysite.com/seller/test/about
actually, it's like this
test.com/about (mysite.com/seller/test/about)
I have this on my Routes.php
$route['default_controller'] = "index";
$route['404_override'] = 'index/lost';
$route['translate_uri_dashes'] = FALSE;
$route['seller/(:any)/product/detail/(:num)'] = 'seller_product/detail/$2';
$route['seller/(:any)/(:any)'] = 'seller/$2/$1/';
$route['seller/(:any)/(:any)/(:num)'] = 'seller/$2/$1/$3';
$route['seller/(:any)'] = 'seller/index/$1';
I hope you understand what I'm going to do
thanks for your help
i hope it will work for you. set all domain path to root directory of your website. and make these changes in your route.php file
switch ( $_SERVER['HTTP_HOST'] ) {
case 'test.com':
$route['default_controller'] = "seller/test";
$route['(.*)'] = "seller/test/$1";
break;
case 'book.com':
$route['default_controller'] = "seller/book";
$route['(.*)'] = "seller/book/$1";
break;
case 'aaa.com':
$route['default_controller'] = "seller/aaa";
$route['(.*)'] = "seller/aaa/$1";
break;
default:
$route['default_controller'] = "index";
break;
}

(:any) routing blocks 404_override routing

I have these routes :
$route['default_controller'] = 'Pages/view/$1';
$route['product/(:any)'] = 'pages/product/$1';
$route['category/(:any)'] = 'pages/category/$1';
$route['(:any)'] = 'pages/view/$1';
but when I want to add $route['404_override'] = 'My404' my any routing stops working and I'm not able to get to my homepage like base_url('home')...
you need to define the route for home controller. Problem is that you have defined $route['(:any)'] = 'pages/view/$1'; which will override all undefined routes. So either you need to change the $route['(:any)'] = 'pages/view/$1'; to something else or need to define the route for home like: $route['home'] = 'home_controller';

codeigniter Different/separate 404 page for frontend and backend

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.

CodeIgniter: Understanding routes

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";

Resources