Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement absolute URLs on localhost and web server?

I've typically used the following linking practice, of relative URLs with an absolute URL path:

<a href="/relative/path/to/document.html">

But I will implement absolute URLs:

<a href="http://example.com/relative/path/to/document.html">

It's not a problem for me to change them (automated find & replace in HTML documents).

But what is the best practice to make sure that it will work on both my localhost (which supports PHP), as well as on the web? And why?


For example, here's how I do PHP includes:

<?php include($_SERVER['DOCUMENT_ROOT']."/relative/path/to/document.html"); ?>

Adopt the same approach for href URLs? Is a different PHP technique better? Curious to hear why.

like image 788
Baumr Avatar asked Jan 17 '13 03:01

Baumr


People also ask

How do you make an absolute URL?

If you prefix the URL with // it will be treated as an absolute one. For example: <a href="//google.com">Google</a> . Keep in mind this will use the same protocol the page is being served with (e.g. if your page's URL is https://path/to/page the resulting URL will be https://google.com ).

What is the absolute URL for a web page?

An absolute URL is the full URL, including protocol ( http / https ), the optional subdomain (e.g. www ), domain ( example.com ), and path (which includes the directory and slug). Absolute URLs provide all the available information to find the location of a page.

Which is an example of an absolute URL?

Absolute URL - An absolute URL contains all the information necessary to locate a resource. In context to the AmeriCommerce online stores system, it typically begins with http:// or https:// . For example, http://sparkpay.com is an absolute URL.


3 Answers

You could just include a predefined constant or variable in the HTML hrefs.

e.g.

<?php
define("LOCAL", "http://localhost");
define("WEB", "http://foo.bar");
$environment = LOCAL; //change to WEB if you're live
?>
<a href="<?php echo $environment; ?>/relative/path/to/document.html">
like image 136
hammus Avatar answered Nov 03 '22 00:11

hammus


Keep the same directory structure locally and on the web host, and use relative URIs and paths whenever possible. If you must use an absolute URI, define it once somewhere and use the constant whenever you need to output a URI to avoid having to make the changes using find and replace.

<?php
    define('ROOT_URI', 'http://example.com/subdirectory');
?>

<a href='<?php echo ROOT_URI; ?>/relative/path/to/document.html'> document </a>

It's easier if you have a configuration file or some other included file where you only define it once.

like image 31
rink.attendant.6 Avatar answered Nov 02 '22 23:11

rink.attendant.6


That all depends on how you plan on hosting the content. PHP is not a bad choice as there are oodles of hosting companies that support PHP. Without a scripting language like PHP, your best bet would be javascript, but depending why you are trying to make the URL's absolute, that might defeat your purpose.

One thing I would suggest is making a configuration file as you may or may not be able to depend on $_SERVER['DOCUMENT_ROOT'].

Say: config.php

<?php
define("HOST", "http://example.com");
// Whatever else you might need on all files.

Then every file that needs it, put this at the top

<?php
include_once(dirname(__FILE__)."/config.php");

// Keep in mind this path is relative to current file, so if you are 3 sub-folder keep
// include_once(dirname(__FILE__)."/../../../config.php")
?>

...

<a href=<?php echo HOST;?>/relative/path/to/document.html">...
like image 21
ymmyk Avatar answered Nov 02 '22 22:11

ymmyk