Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
AcidSlide opened this issue Nov 16, 2019 · 6 comments
Closed

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

AcidSlide opened this issue Nov 16, 2019 · 6 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@AcidSlide
Copy link

AcidSlide commented Nov 16, 2019

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
@AcidSlide AcidSlide added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 16, 2019
@lonnieezell
Copy link
Member

lonnieezell commented Nov 16, 2019 via email

@AcidSlide
Copy link
Author

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.

@AcidSlide
Copy link
Author

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

@AcidSlide
Copy link
Author

@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.

@ChibuezeAgwuDennis
Copy link

ChibuezeAgwuDennis commented Nov 18, 2019

@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

@lonnieezell
Copy link
Member

@AcidSlide great! Glad to hear that.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

3 participants