CRISHK Corporation
CRISHK Corporation

Reputation: 3008

HTML Base URL and links

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

Upvotes: 6

Views: 22044

Answers (8)

when using base url you should not put www. in it.

Next if your url is mysite.com and your app is located at mysite.com/app then you set your base to be mysite.com/app when you use your example info.html it will look like mysite.com/app/info.html.

You can also use ../app/info.html and it will look like mysite.com/app/info.html

Upvotes: 0

WGroleau
WGroleau

Reputation: 455

I would start all link herd with /and the top directory of the URI. That makes them relative to the domain. Then just put the protocol and domain in the base.

Upvotes: 0

Erenor Paz
Erenor Paz

Reputation: 3161

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 :)

Upvotes: 1

George Marshall
George Marshall

Reputation: 327

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.

Upvotes: 6

danieelito
danieelito

Reputation: 51

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/"/>

Upvotes: 4

Shakti Singh
Shakti Singh

Reputation: 86386

Define two constant one for your css, js resources and one for links and use them across the application:-

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

so that if any change in base url you can look up to these constants only.

Upvotes: 0

kobe
kobe

Reputation: 15835

you should have something like this

root 

   root/css
   root/js

   root/files/html1.htm
   root/files/htmml2.htm

root will be nothing but /// your website name

http://www.myhomeurl.com

Upvotes: 0

Pradeep Singh
Pradeep Singh

Reputation: 3634

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>

Upvotes: 2

Related Questions