简体   繁体   English

用 .htaccess 中的 + 替换 URL 编码 (%20)

[英]Replacing URL encoding (%20) with + in .htaccess

Here's what I've got so far which works fine for redirecting single keywords, but not so well if the URL contains a URL encoded space (%20).这是我到目前为止所得到的,它适用于重定向单个关键字,但如果 URL 包含 URL 编码空间 (%20),则效果不佳。

RewriteCond %{QUERY_STRING} (?:^|&)search=([^&]+) [NC]
RewriteRule ^jobs/?$ /?s=%1 [L,NC,R=301]

What I'd like to do is change %20 to be + .我想做的是将%20更改为+

For example, suppose I have the following URL:例如,假设我有以下 URL:

http://www.example.com/jobs/?search=first%20second

I'd like to redirect it to the following:我想将其重定向到以下内容:

http://www.example.com/?s=first+second

Thanks谢谢

...but not so well if the URL contains a URL encoded space (%20). ...但如果 URL 包含 URL 编码空间 (%20),则效果不佳。

 RewriteCond %{QUERY_STRING} (?:^|&)search=([^&]+) [NC] RewriteRule ^jobs/?$ /?s=%1 [L,NC,R=301]

Since the QUERY_STRING is already URL encoded, you need to include the NE ( noescape ) flag on the RewriteRule directive to prevent any URL encoded strings being doubly encoded.由于QUERY_STRING已经是 URL 编码的,您需要在RewriteRule指令中包含NE ( noescape ) 标志以防止任何 URL 编码的字符串被双重编码。 This doesn't just apply to spaces (ie. %20 to %2520 ), but to many other non-alphanumeric characters as well.这不仅适用于空格(即%20%2520 ),也适用于许多其他非字母数字字符。

For example:例如:

RewriteRule ^jobs/?$ /?s=%1 [NE,L,NC,R=301]

Do you still need to replace %20 with + ?你还需要用+替换%20吗? Whether spaces are URL encoded as %20 or + in the query string shouldn't strictly matter to the receiving application.查询字符串中的空格是 URL 编码为%20还是+对接收应用程序来说并不重要。 Both will be URL-decoded as a literal space .两者都将作为文字空间进行 URL 解码。

NB: Only in the query string can a space be URL encoded as + .注意:只有在查询字符串中,空格才能被 URL 编码为+ In other parts of the URL, a + is seen as the literal character (plus).在 URL 的其他部分, +被视为文字字符(加号)。

Replace %20 with + in the search URL parameter valuesearch URL 参数值中用+替换%20

If you want to replace all %-encoded spaces (ie. %20 ) with + (alternative encoding) in the search URL parameter value then you can do it like this:如果要将search URL 参数值中的所有 % 编码空格(即%20 )替换为+ (替代编码),则可以这样做:

Assumptions:假设:

  • Only interested in the "search" URL parameter, since you are currently discarding all other URL parameters anyway.只对“搜索”URL 参数感兴趣,因为您目前正在丢弃所有其他 URL 参数。
  • There can be any number of %-encoded spaces, ie.可以有任意数量的 % 编码空格,即。 %20 , in the URL parameter value. %20 ,在 URL 参数值中。

For example:例如:

# Replace all "%20" in the "search" URL parameter with "+"
# eg. "/jobs?search=foo%20bar%20baz%20pop" to "/jobs?search=foo+bar+baz+pop"
RewriteCond %{QUERY_STRING} (?:^|&)(search)=([^&]*)%20([^&]*) [NC]
RewriteRule ^jobs/?$ $0?%1=%2+%3 [N]

# Redirect "/jobs/?search=<foo>" to "/?s=<foo>"
# (Unchanged from original redirect)
RewriteCond %{QUERY_STRING} (?:^|&)search=([^&]+) [NC]
RewriteRule ^jobs/?$ /?s=%1 [NC,NE,R=302,L]

The first rule repeatedly loops internally until all the occurrences of %20 have been replaced with + in the URL parameter value.第一条规则在内部重复循环,直到所有出现的%20都被 URL 参数值中的+替换。 The N ( next ) flag causes the ruleset to start over again from the top. Nnext )标志使规则集从顶部重新开始。 On Apache 2.4+ you can set an upper (safe) limit on the number of iterations, eg.在 Apache 2.4+ 上,您可以设置迭代次数的上限(安全)限制,例如。 N=20 . N=20 If there is no %20 then this rule is essentially skipped (since the condition will not match on the initial call).如果没有%20则基本上跳过此规则(因为条件在初始调用时不匹配)。

The $0 (dollar-zero) backreference in the substitution string captures the entire URL-path (saves repetition).替换字符串中的$0 (零美元)反向引用捕获整个 URL 路径(节省重复)。 The %1 (percent-one) backreference simply holds the "search" URL parameter name (again, saves repetition) - the first captured group in the last matched CondPattern . %1 (percent-one) 反向引用仅包含“搜索”URL 参数名称(再次保存重复) - 最后匹配的CondPattern 中的第一个捕获组。

The %2 and %3 backreferences hold the values of the 2nd and 3rd captured groups in the last matched CondPattern , ie. %2%3反向引用保存最后匹配的CondPattern 中第二个和第三个捕获组的值,即。 the value before and after the last %20 character sequence in the URL parameter value.前后历时的值%20的URL参数值的字符序列。

This rule needs to be near the top of your .htaccess file.此规则需要靠近.htaccess文件的顶部。

The second rule is the same as your original redirect directive and actually performs the redirect, changing the URL-path and search URL parameter to s .第二条规则与您的原始重定向指令相同,并且实际执行重定向,将 URL-path 和search URL 参数更改为s

You will need to clear your browser cache before testing since the earlier 301 (permanent) redirect will have been cached by your browser.您需要在测试前清除浏览器缓存,因为较早的 301(永久)重定向已被您的浏览器缓存。 Test with 302 (temporary) redirects - to avoid caching issues - and only change to 301 when everything works as intended.使用 302(临时)重定向进行测试 - 以避免缓存问题 - 只有在一切正常时才更改为 301。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM