Welcome Guest, Not a member yet? Register   Sign In
How to set proper base url?
#1

[eluser]peekk[/eluser]
Hello, when I had my site on development environment - it was url:
Code:
testurl.com

Now on production server my codeigniter app's address has to be
Code:
someurl.com/mysite/

I moved it there, and everytime I'm trying to run some function, example
Code:
/home/test
- it gets me into
Code:
someurl.com/home/test
- which is WRONG.

It has to be
Code:
someurl.com/mysite/home/test
- How to fix it? I did set
Code:
$config['base_url'] = someurl.com/mysite/
#2

[eluser]Paulo Carvalho [/eluser]
Move your whole application to mysite folder inside your root space?.
#3

[eluser]NeoArc[/eluser]
Use the HTML <base> tag, and relative paths to your application:

Code:
<head>
  <base href="<?php echo base_url(); //requires the url helper?>" />
  <link rel="stylesheet" href="css/reset.css" />
  <link rel="stylesheet" href="css/demo.css" />
  [script src="js/jquery.js"][/script]
</head>
<body>
  <a href="site/demo/">Demo</a>
&lt;/body&gt;
#4

[eluser]Hampti[/eluser]
Hi there,

you can set base_url manually via config as you already stated. Bad if you use different subdirectories in development and production environment.

My solution for this nuisance is as follows:
In config.php where $config['base_url'] is set:

Code:
// determine protocol - needed if you run http and https
if(isset($_SERVER['HTTPS'])) { $protocol = 'https://'; }
else {$protocol = 'http://'; }

// set protocol and host
$root = $protocol . $_SERVER['HTTP_HOST'];

// $_SERVER['SCRIPT_NAME'] is /subdirs/index.php, basename($_SERVER['SCRIPT_NAME'] is index.php -> replace to get subdirs and append to root
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),'',$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;


Works fine for me, hope it helps

Best Regards
Constantin
#5

[eluser]NeoArc[/eluser]
That's a good solution. Also, Codeigniter 2.1 autodetects the base url if the configuration value is empty.

$config['base_url'] = "";
#6

[eluser]Aken[/eluser]
And since no one actually answered the question directly, the correct base_url format is a full URL to the directory where your index.php file is, including http and a trailing slash (as is properly documented in the comments directly above the config setting - READ). In your production case, it would be http://someurl.com/mysite/




Theme © iAndrew 2016 - Forum software by © MyBB