Link practice without “/” in the beginning of href

為{幸葍}努か 提交于 2019-12-23 04:34:43

问题


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>

回答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.




回答2:


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.




回答3:


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!



来源:https://stackoverflow.com/questions/9221872/link-practice-without-in-the-beginning-of-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!