【发布时间】:2020-11-15 05:58:34
【问题描述】:
我正在生成一个网页。生成器采用字符串形式的页面标题并将其转换为 .html 文件名。例如,如果标题是first,则文件名是first.html。
我了解 URL 路径段只能包含某些字符,而其他所有字符都是 need to be percent encoded。因此,标题为second post 的文件名应为second%20post.html。
问题是文件名以百分比编码的页面无法加载; second%20post.html 是 404。非百分比编码的文件名可以正常工作; first.html、second+post.html 和 second post.html(带空格)将加载。
second post.html加载完毕后,地址栏显示:
http://localhost:8000/second%20post.html
当second%20post.html被加载时,地址栏也显示:
http://localhost:8000/second%20post.html
但给出了 404。
为什么second%20post.html 不加载而second post.html 加载?会不会和文件在磁盘上的存储方式有关?
index.html
<!DOCTYPE html5>
<html lang="en">
<head>
<link href="static/style.css" rel="stylesheet" type="text/css" />
<link rel='shortcut icon' type="image/png" href="static/favicon.png" />
<title>Index</title>
</head>
<body>
<div id="content">
<ul>
<li><p class="post-title"><a href="./first.html">first</a></p></li>
<li><p class="post-title"><a href="./second post.html">second post</a></p></li>
<li><p class="post-title"><a href="./second%post.html">second post</a></p></li>
</ul>
</div>
</body>
</html>
first.html
<!DOCTYPE html5>
<html lang="en">
<head>
<link href="static/style.css" rel="stylesheet" type="text/css" />
<link rel='shortcut icon' type="image/png" href="static/favicon.png" />
<title>First post</title>
</head>
<body>
<div id="content">
<p>First post<p>
</div>
</body>
</html>
'第二个 post.html'
<!DOCTYPE html5>
<html lang="en">
<head>
<link href="static/style.css" rel="stylesheet" type="text/css" />
<link rel='shortcut icon' type="image/png" href="static/favicon.png" />
<title>Second post</title>
</head>
<body>
<div id="content">
<p>Second post, with a space<p>
</div>
</body>
</html>
second%20post.html
<!DOCTYPE html5>
<html lang="en">
<head>
<link href="static/style.css" rel="stylesheet" type="text/css" />
<link rel='shortcut icon' type="image/png" href="static/favicon.png" />
<title>Second post</title>
</head>
<body>
<div id="content">
<p>Second post, percent encoded<p>
</div>
</body>
</html>
【问题讨论】:
-
URL 中的空格被
%20替换,这是正常行为。 -
您在第三个链接中有错字,
second%post而不是second%20post,这就是为什么您有 404。而且 doctype 应该只是<!DOCTYPE html>。 -
@artanik,是的,就是这样。 叹息 谢谢。我应该删除这个问题还是您回答我接受是否合适?
-
@LoremIpsum,我决定回答,因为它可能对其他人有帮助????
-
fwiw,我仍然对实际的生成器有问题。我想我只需要完全剥离它,以便它准确地产生这里所要求的内容。
标签: html