Base URL

5 posts by 3 authors in: Forums > CMS Builder
Last Post: March 18, 2013   (RSS)

By Toledoh - March 13, 2013

Hi Guys,

My understanding is that I should be developing sites with Site Relative URL's, however my host set's up staging servers as something like 111.111.111.111/~sitename/ so all my links are out.

By my reading of things, I should be able to use something like <base href="http://111.111.111.111/~sitename/"> in the <head> tag - but this doesn't seem to work.

Is there a good way of working with this kind of thing?

Cheers,

Tim (toledoh.com.au)

By Dave - March 13, 2013

Hi Tim, 

Those /~sitename/ preview urls can be annoying.  Usually you can move a site from a staging server to production/live server without any changes.  However, with those preview urls you need to change the paths since they'll be different between staging and live.

What I'd recommend is creating a "init" library for all your sites called _app_init.php and put that in the root of your website.  Put any code that you'll be using on most pages in that files such as loading viewer functions, loading common sections, etc.  Also add a line of text to set $BASE_URL which would be either / or /~sitename/ like this:

// Set $BASE_URL for all links, so /~website will be added if using preview urls
$BASE_URL = preg_match("|^/~[^/]+|", @$_SERVER['SCRIPT_NAME'], $matches) ? $matches[0] : '';

Then just use that in all your links like this: 
href="<?php echo $BASE_URL; ?>/contact/contact.php"
src="<?php echo $BASE_URL; ?>/js/library.js"
etc

And if you like (if you have a test site) we can add something to 2.53 to automatically add /~sitename/ to $record['_link']'s so you don't have to keep switching it back and forth.

I'm sure there's a few people who have to do that so it we can make it automatic in the software to save people time and hassle we'd like to do that.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By Toledoh - March 13, 2013

save people time and hassle we'd like to do that

That's why I love you guys:)  Thanks!

Cheers,

Tim (toledoh.com.au)

Hi Dave,

That suggested tweak to deal with the pre-domain sites with URL like: http://111.111.111.111/~account/... would be very welcome indeed!

J