ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Automatic config[base_url]

September 05, 2007 11:19am

Subscribe [27]
  • #1 / Sep 05, 2007 11:19am

    paulopmx's avatar

    paulopmx

    164 posts

    Don’t you just hate it, when you move from development server to production server that you have to change the $config[’base_url’] setting?

    Well I do. Because I do it a lot, specially when I’m demoing a web application, that’s why I added this code to the config.php

    $root = "http://".$_SERVER['HTTP_HOST'];
    $root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
    
    $config['base_url']    = "$root";

    Once thats added, Codeigniter base_url will adapt to whatever the url you’re using.

  • #2 / Sep 05, 2007 11:40am

    frenzal's avatar

    frenzal

    136 posts

    good one, thanks 😊

  • #3 / Sep 19, 2007 1:05pm

    Dready's avatar

    Dready

    65 posts

    improved one :

    $config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
    $config['base_url'] .= preg_replace('@/+$@','',dirname($_SERVER['SCRIPT_NAME'])).'/';

    makes use of dirname and takes care of having only one trailing slash.

  • #4 / Sep 19, 2007 5:40pm

    Michael Wales's avatar

    Michael Wales

    2070 posts

    better one:
    take the 3.6 seconds to change the one variable in config.php

    Just kidding guys - nice addition that has tripped me up many times before. I’ve been completely stumped wondering why my app wasn’t working properly only to find it was 17 little characters destroying my life: http://localhost/

  • #5 / Sep 19, 2007 10:44pm

    Derek Allard's avatar

    Derek Allard

    3168 posts

    3.6 seconds to type that Michael.  Dude, you need to optimize yourself.  I’m down to 3.2 seconds.  And just to show you how fast CodeIgniter is, for me to make the exact change in CakePHP took 5.2 seconds!  Truly CI is fast!

    😊

  • #6 / Sep 20, 2007 9:07pm

    phpMaster

    13 posts

    .... nice addition that has tripped me up many times before.
    I’ve been completely stumped wondering why my app wasn’t working properly
    only to find it was 17 little characters destroying my life: http://localhost/

    Hi!
    I posted a ‘mini article’ in another topic, about same issue:
    http://ellislab.com/forums/viewreply/297477/

    In a bit shorter version:
    —————————————————————————————————————

    Many many many many softwares have not an intelligent way of setting BASE-URL.
    I make 99% of my PHP work and applications admin from http://localhost/
    Now, if I set base-url in a software (sometimes even in the database!)
    like this: http://localhost/app/
    then nobody else but me can use my app.

    If I set base-url like this: http://www.mydomain.com/app/
    then I can not admin my apps (unless I connect via some proxy server)

    This is how I have set config of my CodeIgniter ( and a whole bunch of other less intelligent softwares 😊 )

    //| URL to your CodeIgniter root.  WITH a trailing slash:
    
    $config['base_url']    = "http://".$_SERVER['HTTP_HOST']."/codeigniter/";

    Adding a couple of more lines, it is even possible to detect if it should be either of:
    https:// http:// ... and the phpBB3 developers even auto detect whatever :8080 port number!

    This way it will work whatever URL alias is used to visit your site!
    yourdomain.com, localhost, 127.0.0.1, 192.168.0.1 etc etc
    And if you change your domain name, you wont have to change a thing.
    —————————————————————————————————————

    Regards - phpMaster

  • #7 / Oct 09, 2007 10:41am

    Code Arachn!d's avatar

    Code Arachn!d

    92 posts

    I use this pretty successfully

    $proto = "http" .
        ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "s" : "") . "://";
    $server = isset($_SERVER['HTTP_HOST']) ?
        $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
    $config['base_url']    = $proto . $server;
  • #8 / Oct 25, 2007 6:39pm

    WolfgangA

    46 posts

    Maybe this sort of default value to enable autoconfiguration will make its way into the next release of CI.

  • #9 / Oct 27, 2007 10:29pm

    Phil Sturgeon's avatar

    Phil Sturgeon

    2889 posts

    //| URL to your CodeIgniter root.  WITH a trailing slash:
    
    $config['base_url']    = "http://".$_SERVER['HTTP_HOST']."/codeigniter/";

    Adding a couple of more lines, it is even possible to detect if it should be either of:
    https:// http:// ... and the phpBB3 developers even auto detect whatever :8080 port number!

    This way it will work whatever URL alias is used to visit your site!
    yourdomain.com, localhost, 127.0.0.1, 192.168.0.1 etc etc
    And if you change your domain name, you wont have to change a thing.

    ONLY if you are working in the same directory on both local and live, which you probably wont be. Its a good method (used it myself) but not perfect.

  • #10 / Oct 28, 2007 2:21am

    paulopmx's avatar

    paulopmx

    164 posts

    .... nice addition that has tripped me up many times before.
    I’ve been completely stumped wondering why my app wasn’t working properly
    only to find it was 17 little characters destroying my life: http://localhost/

    Hi!
    I posted a ‘mini article’ in another topic, about same issue:
    http://ellislab.com/forums/viewreply/297477/

    In a bit shorter version:
    —————————————————————————————————————

    Many many many many softwares have not an intelligent way of setting BASE-URL.
    I make 99% of my PHP work and applications admin from http://localhost/
    Now, if I set base-url in a software (sometimes even in the database!)
    like this: http://localhost/app/
    then nobody else but me can use my app.

    If I set base-url like this: http://www.mydomain.com/app/
    then I can not admin my apps (unless I connect via some proxy server)

    This is how I have set config of my CodeIgniter ( and a whole bunch of other less intelligent softwares 😊 )

    //| URL to your CodeIgniter root.  WITH a trailing slash:
    
    $config['base_url']    = "http://".$_SERVER['HTTP_HOST']."/codeigniter/";

    Adding a couple of more lines, it is even possible to detect if it should be either of:
    https:// http:// ... and the phpBB3 developers even auto detect whatever :8080 port number!

    This way it will work whatever URL alias is used to visit your site!
    yourdomain.com, localhost, 127.0.0.1, 192.168.0.1 etc etc
    And if you change your domain name, you wont have to change a thing.
    —————————————————————————————————————

    Regards - phpMaster

    But what if your app is in a subdirectory, maybe even a different subdirectory than the one you used in your development server. That’s why you need to consider the location of your main index.php in relation with the $_SERVER[‘HTTP_HOST’].

  • #11 / Nov 05, 2007 12:02am

    paulopmx's avatar

    paulopmx

    164 posts

  • #12 / Nov 06, 2007 6:23am

    WolfgangA

    46 posts

    Wiki Page

    http://codeigniter.com/wiki/Automatic_configbase_url/

    Thank you.  😊
    However, do you want to add also the part to work with HTTP and HTTPS?

  • #13 / Nov 25, 2007 10:42am

    paulopmx's avatar

    paulopmx

    164 posts

    Sure, haven’t had much time though I think someone replied with a solution for the http and https part, i will incorporate it later, but if you want you can edit the wiki yourself.

  • #14 / Dec 06, 2007 4:08pm

    WolfgangA

    46 posts

    Updated the wiki cause i needed this. Just in case, here the code:

    $config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
    $config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
    $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
  • #15 / Dec 06, 2007 6:19pm

    Thoer's avatar

    Thoer

    111 posts

    Now the main purpose of this post is to finally use the ‘code’ tag here 😊 , but here’s the way I like it and I’m almost sure that I even win microseconds and 0.00001% readablity…

    if ($_SERVER['HTTP']=='localhost') $config['base_url'] = 'http://localhost/path/to/local/folder/';
    else $config['base_url'] = 'http://www.server.tld/path/to/online/folder/';

    Okay, I know I need to set it every time I create a new project… 😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases