Re: Relative URLs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Pedro,

Something has got to give somewhere. You can't have all those requirements without some sort of trade off -- especially if you're dealing with some sort of CMS or with content entered by some non-technical writers. The common solution is to use PHP to write the links for you in some way, using a globally available constant or variable that holds a path prefix, and specifying all paths as absolute URLs relative to that path.

<?php
$prefix = '/foo';
echo "<a href='{$prefix}/to/somewhere.html'>link</a>"
?>
The above example outputs <a href='/foo/to/somewhere.html'>link</a>

<?php
$prefix = '';
echo "<a href='{$prefix}/to/somewhere.html'>link</a>"
?>
But this just outputs <a href='/to/somewhere.html'>link</a>

In the context of HTML the link might look like this:
<a href="<?=$prefix?>/to/somewhere.html">link</a>


And if all that seems too awkward, you could write a helper function which writes the links for you, eg:


<?
function link($text,$uri) {
  global $prefix;
  return "<a href='{$prefix}/{$uri}'>{$text}</a>";
  }
$prefix = "/path";
?>
and then do this:
<?= link("click here","/to/something.html") ?>

Obviously the function could be extended to check for #fragments, but without a doubt, the only safe way to write good URLs is to specify a full path for everything except #fragments. Then if you need the ability to move the site into directory, then specify a path prefix which gets used when needed.

Passing the entire HTMl page through a URL parser/modifier would be another option, but I'd be staying well clear of that.

I could go on for hours and provide plenty of reasons, and better examples, but you need to do some of it yourself :) And if your answer is "I only want to use plain HTML for the links, but still want to be able to move my site into directories", then I'm afraid no one can really help you, sorry.


Justin



On 22/02/2005, at 2:18 AM, Pedro Fayolle wrote:

Hi,

I wanted to gather opinions on what method is best for dealing with
relative URLs (i.e. in HTML links) on complex, multi-level directory
structures.

Here's what I have so far...

Goals:

1. Simpliest possible management of relative URLs.
2. Be able to move the entire site from one directory to another
without breaking links.
3. Be friendly with clean URL methods, such as using special query
strings like "server.com/index.php/products/item/10".

Methods:

Using static absolute URLs ("/absolute/path/") should of course be
considered obsolete as it doesn't comply with goal #2.

One possible method I tend to use is to transform all relative paths
into absolute with PHP before sending the document. This is a very
efficient but rather annoying method to work with, as most URLs need
to be pre-processed.

Another, generally cleaner method is to use the HTML base tag, which
allows one to define a base path for every relative URL found in the
document. This is however a nuisance whenever you need to link back to
the current document, such as with a regular relative query string
("?search=XX") or with a fragment link ("#section"), as the browser
would make them relative to the base path, forcing one to use the long
absolute URL each time.

Please tell me which you generally think is better and why, and if you
know any other method I could've missed.

Thanks,

Pedro Fayolle

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


---
Justin French, Indent.com.au
justin.french@xxxxxxxxxxxxx
Web Application Development & Graphic Design

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux