[英]rewriterule [B] Backrefrence Flag
I am trying to have a snippet in php to understand how B flag works. 我正在尝试在php中了解S标志的工作原理。
My htaccess file location is Applications/AMPPS/www/h or localhost/h having following code 我的htaccess文件位置是Applications / AMPPS / www / h或localhost / h,具有以下代码
RewriteRule search/(.*)$ /search.php?$1 [R,QSA,B]
when I hit url localhost/h/search/x & y/z
with B Flag I got this: localhost/search.php?x+%2526+y%252fz
and without B flag I got this: http://localhost/search.php?x%20&%20y/z
当我使用B标志访问url
localhost/h/search/x & y/z
,我得到了: localhost/search.php?x+%2526+y%252fz
而没有B标志我得到了: http://localhost/search.php?x%20&%20y/z
Now if I use php's urlencode for string x & y/z
that gives me x+%26+y%2Fz
which is replacing space with +
as B Flag does but whereas for &
B flag gives output like %2526
, urlencode gives %26
, when I re-encode the encoded output again, becomes something like: x%2B%2526%2By%252Fz
which yes, encodes %26
into %2526
but encode +
so in both cases , it does not do the way, B does. 现在,如果我对字符串
x & y/z
使用php的urlencode,则得到x+%26+y%2Fz
,它像B Flag那样用+
替换空格,而对于&
B标志则给出%2526
输出,urlencode给%26
,当我再次对编码的输出进行重新编码时,将变为: x%2B%2526%2By%252Fz
,是的,将%26
编码为%2526
但编码+
所以在两种情况下,它都不会这样做,B会这样做。 I need to match it the way B does. 我需要按照B的方式进行匹配。
in addition, what possible characters (may be there can be a list ) where I can find out which non-alphanumeric characters can be encoded by B and how B encode them if I try to understand from php's prospective. 另外,还有什么可能的字符(可能会有一个列表),在这里我可以找出哪些非字母数字字符可以由B编码,以及如果我尝试从php的前瞻性理解中B如何编码它们。
The [B] flag
will encode all non-alphanumeric characters. [B] flag
将编码所有非字母数字字符。 So every character except the 36 (AZ 0-9) or 62 (AZ az 0-9) alphanumeric characters will be escaped. 所以除36(AZ 0-9)或62(AZ AZ 0-9)的字母数字字符的每一个字符将被转义。
So first, why do you need the [B] flag
? 那么首先,为什么需要
[B] flag
? As you said, the string x & y/z
in a URL would result into x%20&%20y/z
which is invalid since &
is one of the reserved characters in URL's even after percent encoding . 如您所说,URL中的字符串
x & y/z
将导致x%20&%20y/z
无效,因为即使百分比编码后, &
也是URL中的保留字符之一 。
So x%20&%20y/z
would only give you x
in your PHP. 因此
x%20&%20y/z
只会在您的PHP中给您x
。
So now you'll need the flag. 因此,现在您需要标记。 Let's analyse what it does with your example string:
让我们分析一下示例字符串的作用:
x & y/z
x%20&%20y/z // encode all spaces (%20)
x%20%26%20y%2Fz // encode the & (%26) and / (%2F)
And this will result in x & y/z
in your PHP. 这将导致PHP中的
x & y/z
。
What you're probably confused about is, that you don't see the correct encoding in your browser. 您可能会感到困惑的是,您在浏览器中看不到正确的编码。
Let's analyse why you get x+%2526+y%252fz
using the [R] flag
: 让我们分析为什么使用
[R] flag
得到x+%2526+y%252fz
:
The &
gets encoded into %26
If you would encode this again, the %
would be encoded into %25
the result would be %2526
. &
被编码为%26
如果再次进行编码,则%
将被编码为%25
,结果将为%2526
。 But why do the both special (reserved) characters get encoded twice? 但是,为什么两个特殊(保留)字符都被编码两次?
This is because the rewrite module does encode the special characters first and then encode everything by default. 这是因为重写模块会先对特殊字符进行编码,然后默认对所有字符进行编码。
If you want to suppress this, you can use the [NE]
flag. 如果要抑制这种情况,可以使用
[NE]
标志。
The behavior of getting +
instead of %20
seems to be a side effect of using [B]
and [R]
since this won't happen if you only use [R]
使用
+
而不是%20
的行为似乎是使用[B]
和[R]
副作用,因为如果仅使用[R]
则不会发生这种情况
To answer your question: the [B] flag
does the same as PHP's rawurlencode . 回答您的问题:
[B] flag
与PHP的rawurlencode相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.