Welcome Guest, Not a member yet? Register   Sign In
Routing and $_SERVER['SCRIPT_NAME'] removal
#1

(This post was last modified: 03-22-2019, 12:54 AM by qwertyman.)

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/g...x-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**
Reply


Messages In This Thread
Routing and $_SERVER['SCRIPT_NAME'] removal - by qwertyman - 03-20-2019, 03:48 AM



Theme © iAndrew 2016 - Forum software by © MyBB