PinoyTech.org

CodeIgniter, Kohana, Mootools, jQuery and CSS

Automatically Get the Base URL for CodeIgniter

Posted by teejay on June 3, 2009

Everytime, we set up or move a Codeigniter site, we have to make sure that the base_url in the config file is correctly set.

Someone recently asked in the CodeIgniter forums about this and we came up with something like this

$config['base_url'] = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off')  ? 'http' : 'https') . '://' . $_SERVER['HTTP_HOST'] . '/';

This snippet of code works but unfortunately doesn't when you've set up CodeIgniter in a subdirectory.

Categories: How To, Web Development

Tags: codeigniter, php

2 Comments

Paulo

Try this:

$url = $_SERVER[‘SCRIPT_NAME’];
$url = substr($url,0,strpos($url,”.php”));
$url = substr($url,0,(strlen($url) - strpos(strrev($url),”/”)));
$url = ((empty($_SERVER[‘HTTPS’]) OR $_SERVER[‘HTTPS’] === ‘off’) ? ‘http’ : ‘https’).”://”.$_SERVER[‘HTTP_HOST’].$url;

$config[‘base_url’] = $url;

July 1st 2009

Teejay Obazee

Sorry about the formatting for comments. I promise I’d fix this when I have the time.

I will also look into your code and try it out.

October 28th 2009

Comments