Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

html - Why do my hyperlinks go to the wrong directory?

In my project functionality is like when i need to set a link to direct to:

/site/sales/sold.php

so the menu.php file link would look like this:

 <ul>
      <li><a href="sales/">Sales</a></li>   
      <li><a href="sold.php">Sold Items</a></li>
    </ul>

it would sometime direct to

/site/sales/sold.php 

and the next moment, it repeats the menu's directory like:

/site/sales/sales/sold.php

so i removed the sales directory, as its directing there by itself, worked for a little while and now it directs to

/site/sold.php

which does not exist so it ends up with a 404.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are on a page sales/ and link to a page sales, the link will result in sales/sales/ – that is the defined behavior.

The default method to avoid that is to specify a base URL via the HTML <base> tag. There is no PHP, JS or CSS involved here – just good ol' HTML 2.0.

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

Using this in your pages head, will define the given URL as base for all links on the page, thus a link like sales/ will actually result in http://www.example.com/site/sales/ when clicked.

See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...