I am trying to routing the url to some Controllers that are in different folder than the url show... I think it is better an example, I have something like this:
$route['admin'] = 'admin/admin';
$route['admin/register'] = 'admin/login/register';
$route['admin/newuser'] = 'admin/login/newuser';
$route['admin/logout'] = 'admin/login/logout';
$route['admin/login'] = 'admin/login';
$route[''] = 'admin/admin';
All of them are working, but last. What I want to do is that someone type www.mydomain.com the system use the controller admin (placed on the folder admin), but I am getting a 404 always.
A fast, but not elegant solution I am thinking (but I wouldn´t want to do) is modifying the 404 file and test if there is no segment, and then redirect.
Any other idea?? Thank you.
You must set default controller for default page when user click you page
$route['default_controller'] = "admin";
it's set your default view for homepage
Step 1: Remove $route[''] = 'admin/admin';
Step 2:
In order to redirect to admin/admin when the user type URL www.mydomain.com, then you should do like this:
$route['default_controller'] = 'admin/admin';
Then it will redirect to admin controller and admin method if no controller after www.mydomain.com.
Have nice routing!
In config/routes.php
$route['default_controller'] = 'admin';
$route['404_override'] = 'admin';
When 404 occur it will load method which you provide in $route['404_override'].
Related
How to redirect from CI default controller to HMVC modules controller?
I have a fresh installation of CI, in which I have defined default controller in /application/config/routes.php
$route['default_controller'] = 'welcome';
Now I am trying to use complete modular way and write everything in modules directory. For that purpose I have created a home app in
/application/modules/home/controllers/Home.php
Now the issue I am having is, I am not able to directly redirect to my home page using localhost/ci
This url is redirecting to default welcome.php
When I pass localhost/ci/index.php/home I can navigate to home page.
I tried using redirect, it didn't work.
I want to redirect to home page using localhost/ci url. Please help.
Change your default controller like this..
$route['default_controller'] = 'home';
if you want to redirect to particular method of your controller:Then
$route['default_controller'] = 'home/method_name';
just change your $route['default_controller'] = 'welcome';
to
$route['default_controller'] = 'folder_name/controller_name';
in your routes file.
and try
How can I remove controller name from url . I have two controller
home and admin
and the url's are
http://domain.com/likes/home/post/sports/20-Athletes-Who-Profited-While-in-College-/12
home/post
and
http://domain.com/likes/admin/ad_managment/edit/2
http://domain.com/likes/admin/meta_tags_home/edit/2
admin/ad_managment admin/meta_tags_home
I have already used this
$route['(:any)/(:any)/(:num)'] = "home/post/$1";
It works for this URL
http://domain.com/likes/home/post/sports/20-Athletes-Who-Profited-While-in-College-/12
admin is not working. Basically I want to remove home/post Leave admin controller it doesn't matter
try this
$route['(:any)/(:any)/(:any)'] = "home/post/$1";
Try this example
$config['base_url'] = 'http://your-site.com/';
And in your routes.php
$route['default_controller'] = 'main';
Through this method you can remove controller name from url.
check user guide of codeigniter and in this example see how the routing done.
Read more about code-igniter routing
I'm building an website using Codeigniter and I've changed the way the URL looks from http://localhost/site_controller/home to http://localhost/home.
I've changed that by using .htaccess -> RewriteBase
changing the $config['index_page'] to $config['index_page'] = '';
and in routes I've change like that:
$route['default_controller'] = "site";
$route['(:any)'] = "site/$1";
And it works fine only for the site controller, BUT my problem is that I have another controller named admin and when I try to login and make a redirect using redirect('admin/index') I get a 404 error.
How can I redirect from site controller to admin controller?
If you are going to route from (:any) => site you have to specify a higher priority route for the admin controller that overrides that route for those pages.
$route['default_controller'] = "site";
$route['admin/(:any)'] = "admin/$1";
$route['(:any)'] = "site/$1";
So I've got a mostly static site. My default controller handles most views in it's index action, simply passing the $this->uri->segment(1) through to my template.
$route['default_controller'] = 'master';
$route['(:any)'] = 'master';
$route['404_override'] = '';
But now I'm implementing a new controller that I would like to have default behavior. And I don't want to be sullying up my routes config with superfluous routes for every single action. So how can I say, route anything to the default master controller except for NewController, which you should handle normally.
Use the 404_override route location to handle your wildcard pages. That way you won't need to define every existing controller/method that you create.
Just make sure that the 404_override controller/method then correctly outputs an HTTP 404 header, and any appropriate output for the browser.
Not totally sure what you mean but if I understand you correctly if you add the line
$route['NewController'] = 'NewController';
In before the (:any) route it should load it first.
$route['default_controller'] = 'master';
$route['NewController'] = 'NewController';
$route['(:any)'] = 'master';
$route['404_override'] = '';
Now, when CodeIgniter does not find a controller for a route or a route for the uri, it shows a generic 404 page. How can I show the accessed route or controller or something useful that would allow me to debug why it's showed a 404?
Code:
switch ($_SERVER['HTTP_HOST']) {
case 'multiplelines.domain.ro':
$route['default_controller'] = "subdomain"; break;
default: $route['default_controller'] = "welcome"; break;
}
$route['scaffolding_trigger'] = "";
$route['404_override'] = '';
if($_SERVER['HTTP_HOST'] == "www.domain.ro"
|| $_SERVER['HTTP_HOST'] == "domain.ro")
{
// URI like '/en/about' -> use controller 'about'
$route['^(romana|english|magyar)/(.+)$'] = "$2";
// '/en', '/de', '/fr' and '/nl' URIs -> use default controller
$route['^(romana|english|magyar)$'] = $route['default_controller'];
}
else
{
}
Desired effect:
When on www or empty subdomain routing will occur like this:
/language/test will go to test controller
When on another subdomain (I just listed one case) will be routing to the subdomain controller as I just want to use one controller for those.
Note: Now when on www or empty subdomain there is code to choose the language automatically for /test route for test controller (without language directory in uri).
When CI dont find correct Route for a page it knows that there isn't such page so it is giving 404 NOT EXISTING..
If user try to open /sgasgasggsasga page at your site it gets 404 for the same reason..
You have to find by yourself why its showing 404...
Look at http://codeigniter.com/user_guide/general/routing.html for help :)
Enable error logging on your application.
Then, once you look up the errors, you will see the URI that was requested that generated the 404 error.
Will fill in answer properly when on computer, but have a look at Debugging routes in codeigniter? it has route logging
How can I show the accessed route or controller or something useful that would allow me to debug why it's showed a 404?
You could echo $_SERVER['REQUEST_URI'] in the 404 view to determine where the user was trying to go.