php 使用查询字符串和锚点标签形成正确的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12682952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:01:25  来源:igfitidea点击:

Proper URL forming with Query String and Anchor Hashtag

phpwordpressurlstandardsurl-fragment

提问by Dan

When both a query string and anchor tag (hash tag) are visible in a URL, what is the proper order for them to appear?

当查询字符串和锚标记(哈希标记)在 URL 中都可见时,它们出现的正确顺序是什么?

http://www.whatever.com?var=val#anchor

http://www.whatever.com?var=val#anchor

or

或者

http://www.whatever.com#anchor?var=val

http://www.whatever.com#anchor?var=val

Is there any documentation on this?

有没有这方面的文件?

update:The URLs are being handled by Wordpress / PHP

更新:URL 正在由 Wordpress/PHP 处理

回答by blitzmann

?var=var#hash

everything after #is client side.

之后的一切#都是客户端。

Also, look into url rewriting to get rid of ugly ?var=var

此外,研究 url 重写以摆脱丑陋 ?var=var

回答by billy

? should come before the #as noted in RFC 3986:

? 应该出现在 # 之前,RFC 3986 中所述

relative-ref = relative-part [ "?" query ] [ "#" fragment ]

Taken from an answer over at superuser (https://superuser.com/questions/498617/does-an-anchor-tag-come-before-the-query-string-or-after):

取自超级用户的回答(https://superuser.com/questions/498617/does-an-anchor-tag-come-before-the-query-string-or-after):

回答by Teacher Michael

Note that when the URL has both anchor tags (#) and query strings (?), the browser may ignore the query string and navigate to the anchor tag without reloading the page.

请注意,当 URL 同时具有锚标记 (#) 和查询字符串 (?) 时,浏览器可能会忽略查询字符串并导航到锚标记,而无需重新加载页面。

It may be necessary to submit the page using a

可能需要使用

    <form action='webpage.php?q=string#tag' method='GET or POST'>
    <input type='text' id='q' name='q' value='string'>
    <input type='submit' value='submit'>
    </form> 

rather than just a URL link

而不仅仅是一个 URL 链接

    <a href='webpage.php?q=string#tag'>.

回答by Matas Vaitkevicius

If intention of using #is to denote page fragmentthen- yes ?then #.

如果使用的意图#是表示页面fragment那么- 是?那么#

If #is coming before ?and it is not to denote page fragment(this can happen when #is part of authority (username and password)) it has to be encoded or you are in trouble. Same applies to any other special characters (:,@,...) that could give different meaning to url.

如果#出现在之前?并且不是表示页面fragment(这可能发生在#权限的一部分(用户名和密码)),则必须对其进行编码,否则您将遇到麻烦。这同样适用于可能赋予 url 不同含义的任何其他特殊字符 ( :, @,...)。