How to implement absolute URLs on localhost and web server?

I usually used the following link: relative URLs with an absolute URL :

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

But I will use absolute URLs :

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

It is not a problem for me to modify them (automatic detection and replacement in HTML documents).

But , what is the best way to ensure that it will work on my localhost (which supports PHP) as well as on the Internet ? And why?


For example, here, as I do PHP,

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

Take the same approach for href urls? Is another PHP method better? Curious to hear why.

+4
source share
5 answers

Keep the same directory structure locally and on the website and use relative URIs and paths if possible. If you must use an absolute URI, define it once somewhere and use a constant when you need to output the URI to avoid having to make 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> 

This is easier if you have a configuration file or some other file in which you only define it.

+5
source

You can simply include a predefined constant or variable in HTML hrefs.

eg.

 <?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"> 
+5
source

It all depends on how you plan to post content. PHP is not a bad choice, as there are hosting hosting companies that support PHP. Without a scripting language such as PHP, javascript would be the best option, but depending on why you are trying to make the URL absolute, that might defeat your goal.

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

Say: config.php

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

Then each 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">... 
+2
source

Instead, you can add the line below to your title. Now you can specify all the URLs relative to your root. Now when you boot to the server or work on localhost. Just change the URL here.

 <base href="http://mydomin.com/" /> 
0
source

Here is a solution that works on both remote and local servers and does not require a domain name change:

 $protocol = isset($_SERVER["HTTPS"]) ? 'https://' : 'http://'; $url_base = $protocol . $_SERVER['HTTP_HOST']; 

With these 2 lines, you will not need to change the code if you change your domain name, and you will not need to change it if you switch between HTTP and HTTPS. Feel free to change the variable names as you wish.

So your link code will look something like this:

 <a href="<?php echo $url_base; ?>/relative/path/to/document.html">link</a> 

IMPORTANT SAFETY UPDATE:

However, there is a security risk with this solution. Here's an article about it .

The variables $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME'] can be changed by sending a different host header when accessing the site:

curl -H "Host: notyourdomain.com" http://yoursite.com/

So any URLs that used $_SERVER['HTTP_HOST'] or $_SERVER['SERVER_NAME'] use notyourdomain.com.

Taking this further, imagine that an attacker fills in the reset form password with your email and changes the host header. Password reset email will direct you to their site. If you paid attention to the domain, youre excellent, but normal users do not and thats why phishing attacks work.

Therefore, use it with caution or better avoid using it.

Here you can use several environments from the related article:

When working with multiple environments, you can use $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME'] , but only to check which domain you enter, and then manually set the correct URL. You can save it with a simple array of valid domains:

 $domains = array('domain.com', 'dev.domain.com', 'staging.domain.com', 'localhost'); if (in_array($_SERVER['HTTP_HOST'], $domains)) { $domain = $_SERVER['HTTP_HOST']; }else { $domain = 'localhost'; } 

You can also make this a little shorter:

 $domains = array('domain.com', 'dev.domain.com', 'staging.domain.com', 'localhost'); $domain = in_array($_SERVER['HTTP_HOST'], $domains) ? $_SERVER['HTTP_HOST'] : 'localhost'; 
0
source

All Articles