Dealing with Absolute and Relative Links (Dev Env)
Moderator: General Moderators
Dealing with Absolute and Relative Links (Dev Env)
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
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)
I set them as constants and switch them depending on whether the site is live or not.. eg
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.
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;
}It doesn't work with any static content or cached stuff though.
- 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)
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/
Actual Search & Replace is my favorite piece of software for this. Check it out! http://www.divlocsoft.com/
- 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)
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)
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.onion2k wrote:I set them as constants and switch them depending on whether the site is live or not..
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)
Not to mention that this would make a mess if done in conjunction with SVN.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 outI'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.
- 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)
astions wrote: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.onion2k wrote:I set them as constants and switch them depending on whether the site is live or not..
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)
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:)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.
Re: Dealing with Absolute and Relative Links (Dev Env)
Changing the code ... to what?
- 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)
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.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:)
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)
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)
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...
- 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)
No, I was just suggesting the astions change his code to make the URI a config option rather than implicitly determining itonion2k 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...
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.
- 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)
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).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 outI'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.