CodeIgniter Forums
Routing and $_SERVER['SCRIPT_NAME'] removal - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: Issues (https://forum.codeigniter.com/forum-19.html)
+--- Thread: Routing and $_SERVER['SCRIPT_NAME'] removal (/thread-73114.html)



Routing and $_SERVER['SCRIPT_NAME'] removal - qwertyman - 03-20-2019

Hi, I have a website built using CodeIgniter 3.1.9.

I have Apache configured with the following RewriteRule as recommended here https://www.codeigniter.com/userguide3/general/urls.html#removing-the-index-php-file so that the index.php part of a url is optional.

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

I noticed that the following url: www.example.com/index.phptest loads the same page as www.example.com/index.php/test. I had a look through the routing code in core/URI.php and saw that the following code was responsible:

Code:
...
if (isset($_SERVER['SCRIPT_NAME'][0]))
{
    if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
    {
        $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
    }
    elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
    {
        $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
    }
}
...

I replaced this code with the following to fix the issue:

Code:
...
if (isset($_SERVER['SCRIPT_NAME'][0]))
{
   if ($uri === $_SERVER['SCRIPT_NAME'])
   {
       $uri = '';
   }
   elseif (strpos($uri, $_SERVER['SCRIPT_NAME'] . '/') === 0)
   {
       $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME'] . '/'));
   }
   elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
   {
       $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
   }
}
...

Additionally, I checked out a list of sites that use CodeIgniter and found the following with the same issue:
**links redacted**


RE: Routing and $_SERVER['SCRIPT_NAME'] removal - InsiteFX - 03-26-2019

If you think this is a bug then you need to report it on Github.

You should never ever edit and change CodeIgniter system files.