【问题标题】:Percent encoded URLs not found未找到编码 URL 的百分比
【发布时间】: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.htmlsecond+post.htmlsecond 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 应该只是 &lt;!DOCTYPE html&gt;
  • @artanik,是的,就是这样。 叹息 谢谢。我应该删除这个问题还是您回答我接受是否合适?
  • @LoremIpsum,我决定回答,因为它可能对其他人有帮助????
  • fwiw,我仍然对实际的生成器有问题。我想我只需要完全剥离它,以便它准确地产生这里所要求的内容。

标签: html


【解决方案1】:

您在第三个链接中有错字,second%post 而不是 second%20post,这就是您有 404 的原因。

无论如何,如果要打开确切的second%20post.html文件,链接应该是second%2520post.html,根据URL encoding method

【讨论】:

  • 再次感谢您。我实际项目中的问题是双重编码。修复似乎只是删除编码步骤并让浏览器处理它。或强制使用有限的字符集。双重编码在这里有更详细的描述:stackoverflow.com/a/16085190/5065796
猜你喜欢
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 2014-01-21
  • 2012-05-21
  • 2014-06-03
  • 1970-01-01
相关资源
最近更新 更多