Dealing with Absolute and Relative Links (Dev Env)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MKayHavoc
Forum Newbie
Posts: 9
Joined: Tue Feb 12, 2008 3:35 am

Dealing with Absolute and Relative Links (Dev Env)

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post 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.
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

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

Post 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/
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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...
}
 
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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:)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post by onion2k »

Changing the code ... to what?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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 :)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post by Benjamin »

It would just be a config option. I'm still trying to figure out what onion was wondering?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post 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...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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.
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

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

Post 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).
Post Reply