为什么我的超链接转到错误的目录? [英] Why do my hyperlinks go to the wrong directory?
问题描述
在我的项目中,功能就像我需要设置一个链接以指向:
/ site / sales / sold .php
因此menu.php文件链接如下所示:
< ul>
< li>< a href =sales />销售< / a>< / li>
< li>< a href =sold.php>已售物品< / a>< / li>
< / ul>
有时会直接转到
<$ p $在下一刻,我们就可以看到,在一段时间内,我们可以看到,它重复菜单的目录,如:
/site/sales/sales/sold.php
并链接到页面
$ c $所以我删除了销售目录,因为它自己的指导工作了一段时间,现在它指向 $ b
$ b/site/sold.php
如果您在页面上<$ c $
c> sales /
sales
,链接将导致sales / sales / $ c
避免这种情况的默认方法是通过HTML
< base>指定一个基本URL。
标签。这里没有涉及PHP,JS或CSS - 只是好的HTML 2.0。< base href =http: //www.example.com/site/\">
在你的页面头部使用这个,将给定的URL定义为页面上所有链接的基础,因此像
sales /
这样的链接实际上会导致http://www.example.com/site/sales/
当点击。
请参阅: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
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.
解决方案If you are on a page
sales/
and link to a pagesales
, the link will result insales/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 inhttp://www.example.com/site/sales/
when clicked.See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
这篇关于为什么我的超链接转到错误的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!