Page 1 of 1

Dealing with Absolute and Relative Links (Dev Env)

Posted: Mon Apr 14, 2008 9:01 am
by MKayHavoc
Hi,

I'm setting up a development box to move away from developing directly on my live box.

The issue i'm having is knowing how best to deal with relative/absolute links.

For example my dynamic navigation has it's links set to e.g.

http://www.mysite.co.uk/marketing/index.php

Obviously when I'm developing on my dev box I don't want my links going off to my live box.

I've never setup a dev box before so i'm not sure about best practices/methods etc.

Does anyone have any advice?

btw, i'm running ubuntu 7.10 and apache.

Cheers

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Mon Apr 14, 2008 9:09 am
by onion2k
I set them as constants and switch them depending on whether the site is live or not.. eg

Code: Select all

switch ($_SERVER['HTTP_HOST']) {
  case "localhost":
  case "192.168.0.1":
    define("SITE_URL","http://localhost/site/version1/");
    break;
 
  case "domain.com":
  case "www.domain.com":
    define("SITE_URL","http://www.domain.com");
    break;
 
}
Then I just use SITE_URL whereever I want the domain and it works automatically.

It doesn't work with any static content or cached stuff though.

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Mon Apr 14, 2008 3:52 pm
by seodevhead
I set up a virtualhost entry in httpd.conf for mydomain.dev. And then I add 'mydomain.dev' to my Windows HOSTS file to point to localhost. That way, not only can you enter 'mydomain.dev' in your browser and get your website, you can also code your scripts using mydomain.dev. Then, before you do any uploading, all you need to do is do a quick "Search and Replace" on all you files... searching='mydomain.dev' and replacing with='mydomain.com'. Then you are good to go!

Actual Search & Replace is my favorite piece of software for this. Check it out! http://www.divlocsoft.com/

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Tue Apr 15, 2008 12:53 am
by Chris Corbyn
You shouldn't have to do a search & replace for something as funamentally changeable as a domain name. It should be defined somewhere like onion points out ;) I'd go so far as to define a complete base URI so that if your website doesn't reside in the document root it will still work.

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Tue Apr 15, 2008 2:09 am
by Benjamin
onion2k wrote:I set them as constants and switch them depending on whether the site is live or not..
I really like onion2k's idea, but I use a different approach. The framework that I have built is intelligent in that it is able to determine all paths by itself which practically eliminates the need for configuring paths.

BUT, in order to ensure that the database username's and passwords on my dev box don't end up in the hands of a client I do something like:

Code: Select all

 
if (file_exists('/var/www/configs/dev_sitename.com.php'))
{
    include '/var/www/configs/dev_sitename.com.php';
} else {
    // define config values here...
}
 

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Tue Apr 15, 2008 2:10 am
by Benjamin
Chris Corbyn wrote:You shouldn't have to do a search & replace for something as funamentally changeable as a domain name. It should be defined somewhere like onion points out ;) I'd go so far as to define a complete base URI so that if your website doesn't reside in the document root it will still work.
Not to mention that this would make a mess if done in conjunction with SVN.

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Tue Apr 15, 2008 3:29 am
by Chris Corbyn
astions wrote:
onion2k wrote:I set them as constants and switch them depending on whether the site is live or not..
I really like onion2k's idea, but I use a different approach. The framework that I have built is intelligent in that it is able to determine all paths by itself which practically eliminates the need for configuring paths.

I have to jump in at this point and tell you that your framework will not work with distributed apps in which case (i.e. it won't work for clusters where everything goes through a reverse proxy). There are ways around it, but I'd strongly suggest changing your code.

Reading from $_SERVER or will not provide meaningful data once the request is proxied into a LAN.

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Tue Apr 15, 2008 3:39 am
by Benjamin
Chris Corbyn wrote:I have to jump in at this point and tell you that your framework will not work with distributed apps in which case (i.e. it won't work for clusters where everything goes through a reverse proxy). There are ways around it, but I'd strongly suggest changing your code.

Reading from $_SERVER or will not provide meaningful data once the request is proxied into a LAN.
No biggie, when (or if) it comes to that it would be a 1 line code fix. The purpose is to save time and it does that for me:)

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Tue Apr 15, 2008 3:39 am
by onion2k
Changing the code ... to what?

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Tue Apr 15, 2008 3:42 am
by Chris Corbyn
astions wrote:No biggie, when (or if) it comes to that it would be a 1 line code fix. The purpose is to save time and it does that for me:)
Do you mean we'd have to edit your framework, or do you have a config option? The best thing to do is make a config setting for the URI, but use the dynamically computed one as a sensible default.

The issue I mention won't be an issue for Squid proxies who are set to "preserve host".

EDIT | I apologise, re-reading your post it sounds as though your framework is just a personal one that's not in the public domain. That puts a slightly different light on things :)

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Tue Apr 15, 2008 4:05 am
by Benjamin
It would just be a config option. I'm still trying to figure out what onion was wondering?

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Tue Apr 15, 2008 4:19 am
by onion2k
Chris said "There are ways around it, but I'd strongly suggest changing your code." with regard to using $_SERVER. I was wondering what he would recommend instead...

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Tue Apr 15, 2008 4:28 am
by Chris Corbyn
onion2k wrote:Chris said "There are ways around it, but I'd strongly suggest changing your code." with regard to using $_SERVER. I was wondering what he would recommend instead...
No, I was just suggesting the astions change his code to make the URI a config option rather than implicitly determining it :) You'd still read from $_SERVER or make use of the apache PHP extension if you were pre-computing it. There just needs to be an override option that's all.

We run a whole bunch of app nodes behind a squid proxy on Amazon's EC2. We have a url configured to use a dynamically computed value by default, but we use a hard-coded one in production due to the proxy.

Re: Dealing with Absolute and Relative Links (Dev Env)

Posted: Tue Apr 15, 2008 2:46 pm
by seodevhead
Chris Corbyn wrote:You shouldn't have to do a search & replace for something as funamentally changeable as a domain name. It should be defined somewhere like onion points out ;) I'd go so far as to define a complete base URI so that if your website doesn't reside in the document root it will still work.
This is ideal for php scripts... but sometimes you gotta do absolute URL's for other things that can't use variables, ie. CSS. But Chris is right, for PHP use a config.php and set a constant for a base URI. But for everything else, Actual Search & Replace is awesome. Relative links can have their drawbacks (especially if you like to copy and paste various rules from one stylesheet to the next, like me).