I can't able to set base path

Hi,
I have one codeigniter project live working fine but local machine not woking fine

if ($_SERVER['SERVER_PORT'] == '443' OR $_SERVER['SERVER_PORT'] == '80')
{
$_base_path = $_SERVER['SERVER_NAME'].str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
        if ($_SERVER['SERVER_PORT'] == '443')
        {
                $base_url = "https://".$_base_path;
        }else{
                $base_url = "http://".$_base_path;
        }
}else{
        $base_url = "http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
}

this is route.php file code

$controllers_dir = "./application/modules/";

$PATH_INFO = '';
if (strlen(getenv('PATH_INFO')) > 1) { // false on my host
    $PATH_INFO = getenv('PATH_INFO');

} elseif (isset($_SERVER['PATH_INFO']) && strlen($_SERVER['PATH_INFO']) > 1) { // false on my host
    $PATH_INFO = $_SERVER['PATH_INFO'];

} elseif (isset($_SERVER["ORIG_PATH_INFO"]) && strlen($_SERVER['ORIG_PATH_INFO']) > 1) { // bingo!
    $PATH_INFO = $_SERVER['ORIG_PATH_INFO'];
} else {
    $PATH_INFO = str_replace($_SERVER["SCRIPT_NAME"], '', $_SERVER["REQUEST_URI"]);
}
$uri = explode ("/", $PATH_INFO);
if (preg_match('/localhost/',$_SERVER["SERVER_NAME"]))
{
        
        if ( count ($uri) == 3 && ! in_array ($uri[2], __get_reserved_uri ($controllers_dir))){
                $url_slug = $uri[2];
                if($url_slug && $url_slug[0] == '_') {
                    $route[$uri[2]] = "search/show/{$uri[2]}";
                }else {
                   $route[$uri[2]] = "profile/index/{$uri[2]}"; 
                }

        }
        if ( count ($uri) == 3 && $uri[2] == 'hit' && ! in_array ($uri[2], __get_reserved_uri ($controllers_dir))){
                $route[$uri[2]] = "search/page_view_hit";

        }
        if ( count ($uri) == 4 && $uri[3] == '' && ! in_array ($uri[2], __get_reserved_uri ($controllers_dir))){
                $url_slug = $uri[2];
                if($url_slug && $url_slug[0] == '_') {
                    $route[$uri[2]] = "search/show/{$uri[2]}";
                }else {
                   $route[$uri[2]] = "profile/index/{$uri[2]}"; 
                }
        }
        if ( count ($uri) == 4 && $uri[3] == 'links' && ! in_array ($uri[2], __get_reserved_uri ($controllers_dir))){
                $route["$uri[2]/$uri[3]"] = "fan/show/{$uri[2]}";

        }
}
 else    
 {
        if ( count ($uri) == 2 && ! in_array ($uri[1], __get_reserved_uri ($controllers_dir))){
                $url_slug = $uri[1];
                if($url_slug && $url_slug[0] == '_') {
                    $route[$uri[1]] = "search/show/{$uri[1]}";
                }else {
                   $route[$uri[1]] = "profile/index/{$uri[1]}";
                }

        }
        if ( count ($uri) == 2 && $uri[1] == 'hit' && ! in_array ($uri[1], __get_reserved_uri ($controllers_dir))){
                $route[$uri[1]] = "search/page_view_hit";

        }        
        if ( count ($uri) == 3 && $uri[2] == '' && ! in_array ($uri[1], __get_reserved_uri ($controllers_dir))){
                $url_slug = $uri[1];
                if($url_slug && $url_slug[0] == '_') {
                    $route[$uri[1]] = "search/show/{$uri[1]}";
                }else {
                   $route[$uri[1]] = "profile/index/{$uri[1]}";
                }

        }
        if ( count ($uri) == 3 && $uri[2] == 'links' && ! in_array ($uri[1], __get_reserved_uri ($controllers_dir))){
                $route["$uri[1]/$uri[2]"] = "fan/show/{$uri[1]}";

        }
            
 }

please any one find the error

i print uri i got this path
Array ( [0] => [1] => knighteous [2] => )

Try this:
I have the following set in my index.php and use the defined constant to change the environment:

<?php 
  // index.php
  define( 'LOCALHOST', 'localhost' === $_SERVER[  'SERVER_NAME' ] );

  $system_path = '/var/www/ci2/CodeIgniter-3-1-4/system';
  if(LOCALHOST):
	$system_path = $_SERVER['DOCUMENT_ROOT'] .'/ci2/CodeIgniter-3-1-4/system';
	$system_path = '/home/john/www/ci2/CodeIgniter-3-1-4/system';
	// echo getcwd();	/home/john/www/amppagemaker.com/public_html
	// echo __DIR__;	/home/john/www/amppagemaker.com/public_html	
	// echo $system_path;
  endif;

  $application_folder = '../application';

// config/config.php
$config['base_url'] = 'https://my-site.com';
if(LOCALHOST):
   $config['base_url'] = 'http://localhost/';
endif;


As far as your ./config/routes.php is concerned it looks as though you are unnnecessarily reinventing the wheel. I use the the buil-in features from:

CodeIgiter Manual on Routing

Also if your http://localhost is not working it may be necessary to edit the .htaccess file.

Try loading the “index.php” and ensure the paths correspond to your localhost settings. CodeIgniter will display any incorrect paths.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.