CSS FireFox 和 Chrome 在 url 中处理 \(反斜杠)的不同行为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10438008/
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-30 03:46:01  来源:igfitidea点击:

Different behaviours of treating \ (backslash) in the url by FireFox and Chrome

cssfirefoxurlgoogle-chromebackslash

提问by Shoaib Nawaz

BACKGROUND

背景

According to my experience when my ubuntu workstation is configured on domain with active directory, the user name created for me was according to the following pattern.

根据我在具有活动目录的域上配置 ubuntu 工作站时的经验,为我创建的用户名符合以下模式。

domain_name\user_name

域名\用户名

Using the userdirextensions of apache on linux will require to use user name in the URL in order to access public_html in the home directory.

在 linux 上使用 apache的userdir扩展将需要在 URL 中使用用户名才能访问主目录中的 public_html。

http://localhost/~domain_name\user_name

http://localhost/~domain_name\user_name

PROBLEM A:

问题一:

Chrome converts all the backslash '\' characters in the URL to forward slash '/' and the resultant url becomes as under that is totally different and always results Not Found.

Chrome 将URL 中的所有反斜杠 ' \' 字符转换为正斜杠 ' /',结果 url 变得完全不同,并且总是结果Not Found

http://localhost/~domain_name/user_name

http://localhost/~domain_name/user_name

Firefox on the other hand does not convert back slash to forward slashso http request to intended target is served by web server.

另一方面Firefox不会将反斜杠转换为正斜杠,因此对预期目标的 http 请求由 Web 服务器提供。

Common solution is to encode back slash in %5C.

PROBLEM B:

问题 B:

If we use a similar path (containing \in path) in CSS @import construct, the import process of css file as HTTP Get Request is failed by reporting 404 error and the URL reported in the 404 error miss the presence of \altogether. It means \is removed from the URL before to invoke GET request against it.

如果我们在CSS @import 构造中使用类似的路径(在路径中包含\),那么CSS 文件作为HTTP Get Request 的导入过程会因报告404 错误而失败,并且404 错误中报告的URL完全错过了\的存在。这意味着\在针对它调用 GET 请求之前从 URL 中删除。

This behavior is common in Firefox and Chrome. But they have uncommon solutions

这种行为在 Firefox 和 Chrome 中很常见。但他们有不常见的解决方案

Firefox needs escaped back slash to work in css import process.

Firefox 需要转义反斜杠才能在 css 导入过程中工作。

@import url("http://localhost/~domain_name\\user_name/path/to/css");

@import url(" http://localhost/~domain_name\\user_name/path/to/css");

Chrome as usual needs an encoded back slash solution.

Chrome 像往常一样需要一个编码的反斜杠解决方案。

@import url("http://localhost/~domain_name%5Cuser_name/path/to/css");

@import url(" http://localhost/~domain_name%5Cuser_name/path/to/css");

  • What is the unified solutions to deal with \in URL?
  • Is there a way to avoid a \to appear in user name?
  • URL中处理\的统一解决方案是什么?
  • 有没有办法避免\出现在用户名中?

回答by Boris Zbarsky

The unified solution to deal with backslash in a URL is to use %5C. RFC 2396 did not allow that character in URLs at all (so any behavior regarding that character was just error-recovery behavior). RFC 3986 does allow it, but is not widely implemented, not least because it's not exactly compatible with existing URL processors.

处理 URL 中的反斜杠的统一解决方案是使用 %5C。RFC 2396 根本不允许在 URL 中使用该字符(因此有关该字符的任何行为都只是错误恢复行为)。RFC 3986 确实允许它,但没有广泛实现,尤其是因为它与现有的 URL 处理器不完全兼容。

Chrome, in particular, does the same thing as IE: assumes you meant a forward slash any time you type a backslash, as you discovered, because that's what Windows file paths do.

特别是 Chrome,它与 IE 做同样的事情:假设您在任何时候键入反斜杠时都表示正斜杠,正如您所发现的,因为这就是 Windows 文件路径的作用。

回答by cafebabe1991

Try using the Slashy add-on in the firefox to help you with it.Here's a link to it.

尝试使用 firefox 中的 Slashy 附加组件来帮助您。这是它的链接。

Slashy

厚颜无耻

回答by Vasudevan Periasamy

This backslash auto conversion issue has fixed in Chrome version >= 53.0.2785.116.

此反斜杠自动转换问题已在 Chrome 版本 >= 53.0.2785.116 中修复。

Now the backslashes are treated properly as %5C.

现在反斜杠被正确处理为%5C