大网数据广告活动
网站运营与故障问答

What is the purpose of leading slash in HTML URLs?

· 52CE 我爱测

I have noticed that some blogs posts have links using a value starting with / in the href . For example: <a href="/somedir/somepage.html">My Page</a> Does the leading / mean the path is starting from the site root? In other words…

问题原文

原帖链接 · Jérôme Verstrynge · CC BY-SA 3.0

I have noticed that some blogs posts have links using a value starting with / in the href.

For example:

<a href="/somedir/somepage.html">My Page</a>

Does the leading / mean the path is starting from the site root?

In other words, if the site URL is www.mysite.com, the effective href value is www.mysite.com/somedir/somepage.html?

Is this a convention accepted in all browsers?

已采纳解答

原帖链接 · Simon Hayter · CC BY-SA 4.0

It's important to start a URL with a / so that the intended URL is returned instead of its parent or child.

Let's say if your browsing /cream-cakes/ then you have a link on the page that has blah.html without the forward slash it is going to attempt to visit page /cream-cakes/blah.html while with the forward slash it'll assume you mean the top level which will be domain.com/blah.html.

Generally, it's best to always use / in my experience as its more friendly when you change the structure of your site, though there is no right or wrong assuming that the intended page gets returned.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2014-01-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。