Skip to content Skip to sidebar Skip to footer

Best Way/practice To Ensure Links Are Going To Proper Location When Not On Root Of Domain?

I've been wondering this for a while now, but what is the best way to ensure that in a web app (RoR, Sinatra, PHP, anything) that when you are creating links (either generating wit

Solution 1:

How about trying to set the base element in the head of you html layout? First, get the URL, eg. in a way Ilya suggests (if PHP is OK for you). After that you can use the base tag as follows:

<basehref="<?=$full_site_url?>" />

That will set the default URL for all the links and the browser will prepend it to every relative link on the page.

Solution 2:

First of all you need to route all your urls through some kind of url re-writer function.

So you no longer do:

<ahref="some/path/">Foo</a>

But instead something like:

<ahref="<%= url_for('some/path') %>">Foo</a>

All the web frameworks out there have a function like this. While they usually do all kinds of magic in there (to do with MVC controller paths and views and what not), at the end of the function (conceptually) they all prepend your url with a "root" (eg "/this/is/where/the/app/is/"), so as to allow you to create urls in your application that are independent of a hard-coded base path.

RoR uses a configuration directive called "relative_url_root".

Symfony (php) uses a configuration directive also called "relative_url_root".

CakePHP uses a configuration directive called "WEBROOT_DIR".

In cases where these frameworks are running on Apache, this value is often calculated dynamically (if you haven't set it explicitly). On other webservers the environment variables are often not available or are incorrect so this value cannot be determined consistently.

Solution 3:

ilya's answer is a good one, but I think a simpler way to do this is just to precede all your links with a leading "/". This will ensure that they are always relative to the root of the domain:

<ahref="/some/thing">Something</a><!-- Always links to www.domain.com/some/thing --><ahref="some/thing">Something</a><!-- Acutal destination depends current path -->

Solution 4:

You can determine everything you need yourself, no need for configs.

Here’s a PHP example (let’s say index.php is your script name):

<?$folder_on_server = substr ($_SERVER['PHP_SELF'], 0, strpos ($_SERVER['PHP_SELF'], '/index.php'));
  $server_name = $_SERVER['SERVER_NAME'];
  if (80 != $_SERVER['SERVER_PORT']) {
    $server_name .= ':'. $_SERVER['SERVER_PORT'];
  }
  $full_site_url = 'http://'. $server_name . $folder_on_server;
?>

Now, you can always make a link like this:

<ahref="<?=$full_site_url?>/some/thing/">Something</a>

See also discussion in comments.

Post a Comment for "Best Way/practice To Ensure Links Are Going To Proper Location When Not On Root Of Domain?"