Forum Moderators: coopster

Message Too Old, No Replies

Using absolute links

To help in moving from test server to production server

         

thesheep

12:32 am on Nov 22, 2004 (gmt 0)

10+ Year Member



I'm wrestling with how best to write my links for a site that has a complex directory structure. All the pages include a header file in the site root, which has the navigation, so I want to have absolute links there, otherwise it will get too complicated. Then there is the added difficulty of having a different url on my testing server, with a couple of added levels of directory structure. I feel I must be missing some neat ways of setting all this up.

I set a constant

define('SITE_ROOT','http://localhost/x/y/...');

which I can then switch on the new server. And then do my links like this:

echo '<a href="'. SITE_ROOT . 'a/b/page.php">Page</a>';

That seems to work OK. Do other people do that?

Then within my header script I also want to check which page is calling it. So for example if I want to check if it is the homepage of the site, I'm trying to do this:

Check if

(1) 'http://' . SITE_ROOT . 'index.php'

is equal to

(2) 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']

But will (2) give me the 'www' bit as well? Is there not a quick way to get the entire URL of the page to compare it - or is there a better way to do all this stuff.

coopster

4:02 pm on Nov 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Setting a constant would be fine and that's where you might run into your 'www' issues. If your site will serve pages based on either www.example.com or example.com, you would be better off building your constant from the $_SERVER['HTTP_HOST'] variable as well. Then make a constant for your index page, too. Piece it all together to compare urls.

Another consideration is to develop a function to build your urls on the fly. You could add protocol, ports, etc. if you wanted.