Codeigniter4: Bug: base_url() and redirect() not honoring baseURL with paths

Created on 16 Nov 2019  Â·  6Comments  Â·  Source: codeigniter4/CodeIgniter4

Describe the bug

Only affects applications that is not served in the root path of the webserver. If the application has a baseURL like: app.baseURL = 'http://localhost/some_path'

base_url()
When calling base_url() with no parameters, it returns the correct baseURL, but calling it with a parameter (for example a route), it removes the path from the baseURL.

redirect()
When calling redirect('a_route_path') or `redirect()->to('a_route_path'), redirects to the root server url with the route path.

TEST 1

[ .env ]
app.baseURL = 'http://localhost/testPath'

[ Routes.php ]
$routes->get('/login', 'Authentication/Login::index');

[ test ]
        log_message('debug', 'base_url(): {base_url}', [ 'base_url' => base_url() ]);
        log_message('debug', 'base_url(\'login\'): {base_url}', ['base_url' => base_url('login')]);

[ log_message ]
DEBUG - 2019-11-17 03:19:56 --> base_url(): http://localhost/testPath
DEBUG - 2019-11-17 03:19:56 --> base_url('login'): http://localhost/login

[ NOTE: 'testPath' was removed when called with a parameter ]

TEST 2

[ test ]
        log_message('debug', 'site_url(): {site_url}', [ 'site_url' => site_url() ]);
        log_message('debug', 'site_url(\'login\'): {site_url}', ['site_url' => site_url('login')]);

[ log_message ]
DEBUG - 2019-11-17 03:23:53 --> site_url(): http://localhost/testPath/
DEBUG - 2019-11-17 03:23:53 --> site_url('login'): http://localhost/testPath/login

[ NOTE: 'site_url()' worked as expected ]

TEST 3

[ test ]
public function index()
{
        return redirect('login');
}

[ redirects_to ]
http://localhost/login

[ NOTE: redirected to http://localhost/login instead of http://localhost/testPath/login ]

TEST 4

[ test ]
public function index()
{
        return redirect()->to('login');
}

[ redirects_to ]
http://localhost/login

[ NOTE: redirected to http://localhost/login instead of http://localhost/testPath/login ]

TEST 5

[ test ]
public function index()
{
        return redirect(base_url('login'));
}

[ redirects_to ]
EXCEPTION ERROR: CodeIgniter\HTTP\Exceptions\HTTPException
SYSTEMPATH/HTTP/Exceptions/HTTPException.php at line 146

TEST 6

[ test ]
public function index()
{
        return redirect()->to(base_url('login'));
}

[ redirects_to ]
http://localhost/login

[ NOTE: redirected to http://localhost/login instead of http://localhost/testPath/login ]

CodeIgniter 4 version

4.0.0-rc.3

Affected module(s)

URL Helper

Expected behavior, and steps to reproduce if appropriate

Calling base_url() or redirect() with the route path will render the return to the correct path based on the baseURL config.

Context

  • OS: Ubuntu 19.04
  • Web server: Apache/2.4.38
  • PHP version: 7.2.24
bug

Most helpful comment

@AcidSlide great! Glad to hear that.

@ChibuezeAgwuDennis Thanks for the code, but the OP says the latest changes have fixed things up for him.

All 6 comments

Please pull down the latest develop branch and try again. We pushed up some potential fixes for that a couple of days ago.

Sent from my iPhone

On Nov 16, 2019, at 1:36 PM, Rikki Vizcarra notifications@github.com wrote:


Describe the bug

Only affects applications that is not served in the root path of the webserver. If the application has a baseURL like: app.baseURL = 'http://localhost/some_path'

base_url()
When calling base_url() with no parameters, it returns the correct baseURL, but calling it with a parameter (for example a route), it removes the path from the baseURL.

redirect()
When calling redirect('a_route_path') or `redirect()->to('a_route_path'), redirects to the root server url with the route path.

TEST 1

[ .env ]
app.baseURL = 'http://localhost/testPath'

[ Routes.php ]
$routes->get('/login', 'Authentication/Login::index');

[ test ]
log_message('debug', 'Base URL: {base_url}', [ 'base_url' => base_url() ]);
log_message('debug', 'Base URL: {base_url}/login', ['base_url' => base_url('login')]);

[ log_message ]
DEBUG - 2019-11-17 03:19:56 --> base_url(): http://localhost/testPath
DEBUG - 2019-11-17 03:19:56 --> base_url('login'): http://localhost/login

[ NOTE: 'testPath' was removed when called with a parameter ]
TEST 2

[ test ]
log_message('debug', 'site_url(): {site_url}', [ 'site_url' => site_url() ]);
log_message('debug', 'site_url(\'login\'): {site_url}', ['site_url' => site_url('login')]);

[ log_message ]
DEBUG - 2019-11-17 03:23:53 --> site_url(): http://localhost/testPath/
DEBUG - 2019-11-17 03:23:53 --> site_url('login'): http://localhost/testPath/login

[ NOTE: 'site_url()' worked as expected ]
TEST 3

[ test ]
public function index()
{
return redirect('login');
}

[ redirects_to ]
http://localhost/login

[ NOTE: redirected to http://localhost/login instead of http://localhost/testPath/login ]
TEST 4

[ test ]
public function index()
{
return redirect()->to('login');
}

[ redirects_to ]
http://localhost/login

[ NOTE: redirected to http://localhost/login instead of http://localhost/testPath/login ]
TEST 5

[ test ]
public function index()
{
return redirect(base_url('login'));
}

[ redirects_to ]
EXCEPTION ERROR: CodeIgniter\HTTP\Exceptions\HTTPException
SYSTEMPATH/HTTP/Exceptions/HTTPException.php at line 146
TEST 6

[ test ]
public function index()
{
return redirect()->to(base_url('login'));
}

[ redirects_to ]
http://localhost/login

[ NOTE: redirected to http://localhost/login instead of http://localhost/testPath/login ]
CodeIgniter 4 version

4.0.0-rc.3

Affected module(s)

URL Helper

Expected behavior, and steps to reproduce if appropriate

Calling base_url() or redirect() with the route path will render the return to the correct path based on the baseURL config.

Context

OS: Ubuntu 19.04
Web server: Apache/2.4.38
PHP version: 7.2.24
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

Please pull down the latest develop branch and try again. We pushed up some potential fixes for that a couple of days ago.
…

Hi @lonnieezell I can't with the current project i'm having issue with, since it's a new system for a client. For now, my work-around are to call redirects using site_url(). I hope that a new RC or better yet v4 would finally be released.

Only reason I'm using CI4 vs CI3, I liked where CI4 is going and it's already in it's RC stage. Which I was hoping that by the time the new system is in it's final stages, v4 version will be released.

Only reason I'm using CI4 vs CI3, I liked where CI4 is going and it's already in it's RC stage. Which I was hoping that by the time the new system is in it's final stages, v4 version will be released.

Or is this too much wishful thinking!?? ahahahaha

@lonnieezell I've cloned CI4 and merged the updated framework with my project files. (I just hope nothing got broken on my project.)

Based on testing, base_url() and redirect()->to() are working now as expected.

@AcidSlide I solved the issue by creating uri_helper.php file then add this precious codes to it and then retouch my codeigniter.php file

url_helper.php in helper folder

    if (! function_exists('current_url'))
    {
        /**
         * Current URL
         *
         * Returns the full URL (including segments) of the page where this
         * function is placed
         *
         * @param boolean $returnObject True to return an object instead of a strong
         *
         * @return string|\CodeIgniter\HTTP\URI
         */
        function current_url(bool $returnObject = false)
        {
            $uri = remove_host_http(\CodeIgniter\Config\Services::request()->uri);

            $uri = (string) site_url($uri);

            $uri = $returnObject ? $uri : remove_host_http($uri);

            return $uri;
        }
    }

    //--------------------------------------------------------------------

    if (! function_exists('remove_host_http'))
    {
        function remove_host_http($uri = null)
        {
            $uri = str_replace('https://'. $_SERVER['HTTP_HOST'].'/', '', $uri);
            $uri = str_replace('http://'. $_SERVER['HTTP_HOST'].'/', '', $uri);

            return $uri;
        }
    }

    //--------------------------------------------------------------------

in Codeigniter.php in line 978 replace it to this

/**
     * If we have a session object to use, store the current URI
     * as the previous URI. This is called just prior to sending the
     * response to the client, and will make it available next request.
     *
     * This helps provider safer, more reliable previous_url() detection.
     *
     * @param \CodeIgniter\HTTP\URI $uri
     */
    public function storePreviousURL($uri)
    {
        // Ignore CLI requests
        if (is_cli())
        {
            return;
        }
        // Ignore AJAX requests
        if (method_exists($this->request, 'isAJAX') && $this->request->isAJAX())
        {
            return;
        }

        // This is mainly needed during testing...
        if (is_string($uri))
        {
            $uri = new URI($uri);
        }

        $uri = remove_host_http($uri);

        $uri = (string) site_url($uri);

        if (isset($_SESSION))
        {
            $_SESSION['_ci_previous_url'] = (string) $uri;
        }
    }

I hope this will work for you

@AcidSlide great! Glad to hear that.

@ChibuezeAgwuDennis Thanks for the code, but the OP says the latest changes have fixed things up for him.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lonnieezell picture lonnieezell  Â·  6Comments

MisterAnmar picture MisterAnmar  Â·  5Comments

TatwiraT picture TatwiraT  Â·  6Comments

lonnieezell picture lonnieezell  Â·  7Comments

bagimuka picture bagimuka  Â·  5Comments