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

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

By Dave - March 18, 2013

Hi Tim & Jayme, 

I've got a beta on that feature for 2.53.  I'll send you a zip so you can try it.  It's a new field called "Website Prefix URL" in the admin.  You can set it and it adds it to everything, and remove it and it's gone. So this supports urls like /~username/file.php and also developers who have a directory structure like this:
http://www.webdesignco.example/clients/megacorp-abc/index.php

Viewer links are automatically updated and we also make a global available like this: <?php echo PREFIX_URL ?>

So you can have a tag like this: <img src="<?php echo PREFIX_URL ?>/images/myimage.jpg" /> and it will work on any development or live server url: 
http://www.webdesignco.example/clients/megacorp-abc/images/myimage.jpg
http://www.webdesignco.example/~megaco/images/myimage.jpg
http://www.megacorp-123.com/images/myimage.jpg

Hope that makes sense.  It will once you try it.  

Let me know any questions.

Dave Edis - Senior Developer
interactivetools.com