问题
I'm new to HTML world.
I created a "example.html" web page at server,
which contains following a tag links.
...
<nav>
<ol>
<li><a href="http://localhost/link1.html">link1</a></li>
<li><a href="http://localhost/link2.html">link2</a></li>
<li><a href="http://localhost/link3.html">link3</a></li>
</ol>
</nav>
...
If I open this "http://localhost/example.html"
page on server's web browser and click on links(link1~link3), they work.
The problem is that they DO NOT WORK on client's web browser.
I port-forwarded my server computer and opened "example.html"
from client,
through "http://myaddress.com:1234/example.html"
Temporarily, I solved it by changing
every link in server's code, "http://localhost/~~~.html"
to "http://myaddress.com:1234/~~~.html"
.
However, I want to enable client to access every link in server's code written as "http://localhost/~~~.html"
Is it the problem of port-forwarding? or something else?
Thank you so much.
回答1:
This should solve the problem, you don't need localhost in your href's. Remove them from your anchor tags.
<nav>
<ol>
<li><a href="/link1.html">link1</a></li>
<li><a href="/link2.html">link2</a></li>
<li><a href="/link3.html">link3</a></li>
</ol>
</nav>
OR
Try attaching the port to the localhost href? The client must be running it locally for this to work.
<nav>
<ol>
<li><a href="http://localhost:1234/link1.html">link1</a></li>
<li><a href="http://localhost:1234/link2.html">link2</a></li>
<li><a href="http://localhost:1234/link3.html">link3</a></li>
</ol>
</nav>
回答2:
Always use relative paths, not absolute ones:
<nav>
<ol>
<li><a href="link1.html">link1</a></li>
<li><a href="link2.html">link2</a></li>
<li><a href="link3.html">link3</a></li>
</ol>
</nav>
As the name suggests, localhost
points to the local machine. So when you include it to form an absolute URL, it always looks for those files on the local machine. Since those files exist on the server, they work from the server's browser, but when you run it on any other computer, they fail because the files don't exist on that other computer.
来源:https://stackoverflow.com/questions/45128720/links-of-href-http-localhost-in-html-code-at-server-cannot-be-reached-fr