Question

I am trying to create custom routes for my CodeIgniter site. Even the most basic routes do not work. For example I have the welcome controller mapped to "test" and it just 404's on me. I am running on MAMP with mod_rewrite enabled.

I have the index.php line in config.php empty..

$config['index_page'] = '';

Here is my .htacess file..

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA] 

And here is my route..

$route['welcome'] = 'test';
Was it helpful?

Solution

In a route, the array key contains the URI to be matched, while the array value contains the destination it should be re-routed to.

-- CI Documentation

Your physical controller name is welcome. So if you want a URL containing the word test in the first segment be remapped to the welcome class, you should do this:

$route['test'] = "welcome/METHOD";

Where METHOD is method of welcome class.

Note: If class/method was welcome/index, you do NOT need to append /index.

OTHER TIPS

If I read this correctly, that route will try to redirect the controller 'welcome' to the controller 'test'. If you have a controller named 'test' and a function named 'index', you can route the following:

route['welcome/index'] = 'test/index';

Is that what you are trying to do?

A couple things:

  1. Routes in CI are cascaded, they're evaluated top to bottom, and the router stops at the first match. Make sure, then, to have any custom route placed below the 2 default routes in a vanilla distribution:

    $route['default_controller'] = "welcome";
    $route['404_override'] = '';
    // custom routes here
    $route['welcome'] = "welcome/test";
    
  2. If you have a controller named "welcome" (the default one), and you want to call a method named "test", you need a route like

    $route['welcome'] = "welcome/test"
    

    which will be accessible at the url http://wwww.yourdomain.com/welcome (if no route were specified, you would have accessed it like http://www.yourdomain.com/welcome/test)

Usually, controller have an index method which is called automatically when no other method is provided. The route you've created so far isn't working because it's calling the index() method of a "test" controller, which is likely not present.

A suggestion: if you mainatain the "welcome" controller as the default one, and you want to call an url like http://www.yourdomain.com/test You need a test() method and your route must be

$route['test'] = "welcome/test";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top