最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - HTML Base URL and links - Stack Overflow

programmeradmin2浏览0评论

I have the following problem:

The URL is / and the base is:

<base href="/"/>

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

.css
.js
.jpg

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

.html
.html
.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:

.html

and not to:

.html

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

Share Improve this question edited Dec 7, 2010 at 8:10 CRISHK Corporation asked Dec 7, 2010 at 8:02 CRISHK CorporationCRISHK Corporation 3,0086 gold badges39 silver badges53 bronze badges 2
  • your application files should start from root and it should exanpand// – kobe Commented Dec 7, 2010 at 8:10
  • Are you asking for the site to be created or an existing links in existing site? I am asking because it should be known – Shakti Singh Commented Dec 7, 2010 at 8:11
Add a comment  | 

8 Answers 8

Reset to default 6

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.

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

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>

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

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

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.

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.

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

发布评论

评论列表(0)

  1. 暂无评论