不带“/”的链接练习在 href 的开头

发布于 2025-01-03 20:12:00 字数 799 浏览 3 评论 0原文

我使用这个重写代码能够像这样链接 http://page.com/subpage 但仍然可以使用这样的 html 文件 http://page.com/subpage .html(所有 html 文件都位于根文件夹中)。

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]

拥有这样的菜单(子页面上没有“/”)是一个好习惯吗?

<a href="/">Home</a>
<a href="subpage-1">Subpage 1</a>
<a href="subpage-2">Subpage 2</a>
<a href="subpage-3">Subpage 3</a>

如果可以在没有“/”的情况下进行链接,那么链接“主页”链接的最佳方式是什么?

这就是我打算作为菜单的内容(如果您不同意,请告诉我为什么不这样做的理由):

<a href="/">Home</a>
<a href="subpage-1">Subpage 1</a>
<a href="subpage-2">Subpage 2</a>
<a href="subpage-3">Subpage 3</a>

I am using this rewrite code to be able to link like this http://page.com/subpage but still be working with html files like so http://page.com/subpage.html (all html files are located in the root folder).

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Would it be good practise to have a menu like this (without the "/" on the subpages)?

<a href="/">Home</a>
<a href="subpage-1">Subpage 1</a>
<a href="subpage-2">Subpage 2</a>
<a href="subpage-3">Subpage 3</a>

If it's OK to link without "/" what is the best way to link the "home" link?

This is what I am planing to go with as menu (please give me reasons why not to if you have nay):

<a href="/">Home</a>
<a href="subpage-1">Subpage 1</a>
<a href="subpage-2">Subpage 2</a>
<a href="subpage-3">Subpage 3</a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄意 2025-01-10 20:12:00

完全使用 subpage-1 url 并不是一个好习惯,因为您稍后可能会实现重定向。如果您坚持在文件夹中工作,可以使用 ./subpage-1./ 作为主页链接。

It is not good practise to use subpage-1 urls exactly becouse of redirections you might implement later on. If you insist on working in a folder, you can use ./subpage-1 and ./ for the home link.

無心 2025-01-10 20:12:00

在这种情况下,我通常会把一切都绝对化(前提是你合理地可以),只是因为你造成混乱的可能性远远太高了。如果你把事情看成相对的,你肯定会犯错。

如果您的项目超过 30 页,那么我肯定会重新考虑这一点,如果您使用某种脚本语言(PHP、ASP 等),那么您应该利用它来完成您的工作更轻松。

I'd generally make everything absolute in this kind of situation (providing that you reasonably can), just because the potential for confusion on your part is far too high. If you make things relative, you will get one wrong, guaranteed.

If your project is 30+ pages, then I'd definitely have a re-think on that, and if you're using some kind of scripting language (PHP, ASP, etc) then you should make use of that to make your job easier.

浅语花开 2025-01-10 20:12:00

我总是将 / 保留在任何像这样的 href 前面。


使用相对路径一开始就是一个坏习惯,并且会导致以后的灾难。如果您从相对网址开始,后来发现需要它们,那么添加起来会很痛苦。每个链接多一个字符并不是世界末日。

假设我有一个包含这 2 个页面的网站:

/home/ryan/example.com/index.html
/home/ryan/example.com/public.html

并且每个页面的 html 内容都是相同的:

index.html & public.html

<a href="/">Home</a>
<a href="/public">Public</a>

现在,如果我想添加一个新文件,/home/ryan/example.com/special/secret.html 我可以重新使用现有代码,甚至添加一个新链接,同时仍然使相同的代码适用于所有 3 个页面:

所有 html 页面

<a href="/">Home</a>
<a href="/public">Public</a>
<a href="/special/surprise">Surprise</a>

考虑将来的维护并想想未来懒惰的自己。能够重用代码是重点;您不想在最多 15 个位置添加/编辑/删除略有不同的链接,即使 2 个位置也太多了!

I always keep the / in front of any hrefs like this.


Using relative paths is a bad habit to begin with and a recipe for disaster later on. If you start with relative urls and find you need them later on, it'll be a big pain to add. Having one extra character per link isn't the end of the world.

Say I have a site with these 2 pages:

/home/ryan/example.com/index.html
/home/ryan/example.com/public.html

And the html content of each was the same:

index.html & public.html

<a href="/">Home</a>
<a href="/public">Public</a>

Now if I want to add a new file, /home/ryan/example.com/special/secret.html I can re-use that existing code, and even add in a new link while still making the same code work for all 3 pages:

all html pages

<a href="/">Home</a>
<a href="/public">Public</a>
<a href="/special/surprise">Surprise</a>

Consider maintenance down the road and think of your future lazy self. Being able to re-use code is the whole point; you don't want to add/edit/delete slightly different links from up to 15 places, even 2 places is too much!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文