Is this good link practice?

本秂侑毒 提交于 2019-12-13 02:38:40

问题


Is this bad practise in any way?

The folder structure (every html page has filename index.html located in a subfolder):

root-folder (index.html)
  subpage1 (index.html)
  subpage2 (index.html)
  subpage3 (index.html)

Index.html in root folder will have menulinks and like this:

<a href="/">index</a>
<a href="/subpage1">subpage1</a>
<a href="/subpage2">subpage2</a>
<a href="/subpage3">subpage3</a>

All other index.html files in the subfolders/subpages will have menulinks like this:

<a href="../">index</a>
<a href="../subpage1">subpage1</a>
<a href="../subpage2">subpage2</a>
<a href="../subpage3">subpage3</a>

Links to css, javascript, favoicon will also have this link structure.

I think the cleanest way would be to have links like this on every page. But it's complicated to work with if you want to try out the page in a subfolder...

<a href="/">index</a>
<a href="/subpage1">subpage1</a>
<a href="/subpage2">subpage2</a>
<a href="/subpage3">subpage3</a>

回答1:


The problem I have with an approach like this is that all your files are called 'index.html' and it's difficult to tell in your editor which is which. Also you have the problem of opening/closing each folder to navigate around and make edits, yuck.

What you can do to get the url's that you want is to use something like mod_rewrite to turn those nice-urls into the real filenames.

Add something like this for your .htaccess file:

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

And in all your pages you'll be able to have links like the last example:

<a href="/">index</a>
<a href="/subpage1">subpage1</a>
<a href="/subpage2">subpage2</a>
<a href="/subpage3">subpage3</a>



回答2:


The cleanest way, like you said, is to use absolute paths, as in your second example. I would avoid using relative paths for the purpose that you have described.



来源:https://stackoverflow.com/questions/9221100/is-this-good-link-practice

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