HTML Base URL and links

19,847

Solution 1

You could use a base tag and change it so all urls can be relative to the applications base.

<!DOCTYPE HTML>
<html>
  <head>
    <base href="http://www.myhomeurl.com/application1/" />
  </head>
  <body>
    <!-- content -->
  </body>
</html>

Additional information can be found at http://www.w3schools.com/tags/tag_base.asp

edit: w3c spec on base tag http://www.w3.org/TR/html4/struct/links.html#h-12.4 also illustrates how to pull images from other locations.

Solution 2

Change like this on your resources

<link href="./css/myfile.css" rel="stylesheet" type="text/css" />   

Not like

<link href="http://www.myhomeurl.com/css/myfile.css" rel="stylesheet" type="text/css" />

And your base it looks like this

<base href="http://www.myhomeurl.com/application1/"/>

Solution 3

Use this varriable in link HTTP_SERVER

define('HTTP_SERVER', 'http://www.myhomeurl.com/application1/');

<a href="<?php echo HTTP_SERVER?>myurlpage/page1.html">Click me!</a>

Solution 4

I'm a little late for this question, but I can see none of the answers really do what (I think) you need.

I suggest you to use (as others have already said) a Base Url like this:

<base href="http://www.myhomeurl.com/application1/" />

This way your links will work in the intended way. Then, when you add resources, you only have to go up to the parent directory, like the following:

<link href="../css/myfile.css" rel="stylesheet" type="text/css" />

Or

<style type="text/css">
  @import url(../css/myfile.css);
</style>

Note the ../, telling the browser to exit from "application1" directory, going up one step in the file structure.

Hope this helps :)

Share:
19,847
CRISHK Corporation
Author by

CRISHK Corporation

Updated on June 17, 2022

Comments

  • CRISHK Corporation
    CRISHK Corporation almost 2 years

    I have the following problem:

    The URL is http://www.myhomeurl.com/application1/ and the base is:

    <base href="http://www.myhomeurl.com/"/>
    

    All resources like images, css and javascript will be at:

    http://www.myhomeurl.com/css/myfile.css
    http://www.myhomeurl.com/js/myscript.js
    http://www.myhomeurl.com/images/img.jpg
    

    BUT, the link will be at "application1", for example:

    http://www.myhomeurl.com/application1/page1.html
    http://www.myhomeurl.com/application1/page2.html
    http://www.myhomeurl.com/application1/page3.html
    

    The question is: How to apply base URL for resources (like css, js, etc) and apply the base/application1 for page links?

    Here is a problem when I have:

    <a href="page1.html">Click me!</a>
    

    When the user clicks this the page will going to:

    http://www.myhomeurl.com/page1.html

    and not to:

    http://www.myhomeurl.com/application1/page1.html

  • CRISHK Corporation
    CRISHK Corporation over 13 years
    I have the base tag... but the resources ... aren't in application1/