skip to Main Content

Hi guys please advice me as Code igniter 3.1.11 router.php is not working
Its only working for http://maindomain/
but not working for http://maindomain/about instead shows 404 page

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$route['(:any)'] = 'pages/view/$1';


$route['default_controller'] = 'pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

.htaccess file below

RewriteEngine on
RewriteCond $1 !^(index.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
# php -- BEGIN cPanel-generated handler, do not edit
# This domain inherits the “PHP” package.
# php -- END cPanel-generated handler, do not edit

2

Answers


  1. $route['(:any)'] = 'pages/view/$1'; will redirect all your requests to pages/view irrespective of what controller(URL) you write and will be a nightmare to manage all the requests to your site.

    Say, you write
    http://maindomain/xyz/something now because it doesn’t expect any parameter after (:any), it goes to 404

    but
    http://maindomain/xyz will work fine as it satisfies your routing rule.

    The reason http://maindomain works is because you’ve defined it as your default controller. You can write your routes like this –

    $route['default_controller']   = 'pages/view';
    $route['pages/(:any)']         = 'pages/view/$1'; // this will route any requests to pages/xyz to pages/view/xyz
    $route['404_override']         = '';
    $route['translate_uri_dashes'] = FALSE;
    

    See if it helps you.

    Login or Signup to reply.
  2. This looks like a similar issue I had and I ended up fixing it by editing system/core/Router.php and changing line 396 from:

    $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
    

    to

    $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
    

    … not ideal because it isn’t upgrade safe but it did the trick for me (CI 3.1.11, PHP 7.4)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search