Welcome Guest, Not a member yet? Register   Sign In
Forced to use relative base_url
#1

[eluser]tokyotech[/eluser]
I have my base_url set to "../". I can't use an absolute base_url because I'm using iFrames to do fake-AJAX image uploads. Using an absolute base_url would give permission denied errors if the user got to my site with a slightly different base URL (ex: http://www.site.com VS http://site.com VS www.site.com).

So far it's been working out fine because I've only been using the index() function of each controller. However, now I want to have a controller's method load a view (index.php/myController/myMethod). If I use this kind of method, the base_url will incorrectly go one level back to index.php/myController/.

What's the remedy?
#2

[eluser]InsiteFX[/eluser]
You can use it you just need this in your .htaccess file, I posted this before in the forums.

Code:
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.domain.net/$1 [R=301,L]

Enjoy
InsiteFX
#3

[eluser]tokyotech[/eluser]
Is there a way to do this without htaccess? It is quite slow because it requires an additional disk access.
#4

[eluser]tokyotech[/eluser]
A relative link would also make the project more portable. No one would have to change the base_url to get it to work on their machine. Someone told me it could be made relative in PHP.ini, but how?
#5

[eluser]Yorick Peterse[/eluser]
[quote author="tokyotech" date="1253678222"]Is there a way to do this without htaccess? It is quite slow because it requires an additional disk access.[/quote]

Using an .htaccess file isn't slower at all.
#6

[eluser]cahva[/eluser]
"Quite slow" is an overstatement. Its not that slow. And if you want to get rid of the additional disk access, you can define rewrite rules in the apache config so its in the memory.

BTW, cant you just set your base_url to "/"? (or "/yourapp/" depending where your app is). I dont use full url at all and it's been working great.
#7

[eluser]jedd[/eluser]
I think if you've sold your soul and condemned your users to suffering frames .. it's a tad disingenuous to talk about portability or performance.
#8

[eluser]cahva[/eluser]
@jedd: he/she is using iframes for upload stuff so I dont think the devil is coming to collect anytime soon Smile
#9

[eluser]WanWizard[/eluser]
You should always avoid hardcoding anything (bad practice), and that includes the base_url.

In the config.php of our CI projects, base_url is defined as:
Code:
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your ExiteCMS root. Typically this will be your base URL,
|
*/
$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . ROOT;

The constant ROOT is defined in the index.php, as:
Code:
define('ROOTPATH', realpath(dirname(__FILE__)) . '/');

// installed in the docroot?
if (realpath(dirname(__FILE__)) == $_SERVER['DOCUMENT_ROOT'])
{
    define('ROOT', '/');
}
else
{
    define('ROOT', substr(ROOTPATH, strlen($_SERVER['DOCUMENT_ROOT'])+1));
}
This makes your base_url simply follow whatever hostname you used to connect to the website, and it also allows you to move your application to a subdirectory of the webserver DOCROOT without breaking it.
#10

[eluser]tokyotech[/eluser]
WanWizard,

Your thing didn't work on my setup. It somehow sliced away the middle portion of my file structure. This works for me both on localhost and webserver:

Code:
$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") .
    $_SERVER['HTTP_HOST'] .
    substr($_SERVER['SCRIPT_NAME'], 0, strlen($_SERVER['SCRIPT_NAME']) - strlen($config['index_page']));




Theme © iAndrew 2016 - Forum software by © MyBB