保留.htaccess中的URL中的和符号。

huangapple go评论63阅读模式
英文:

Preserve ampersand sign in URL from .htaccess

问题

我在.htaccess中有这个最终规则:

RewriteRule ^(.*)$ index.php?query=$1 [NC,L,QSA]

然后我输入URL:

domain.com/docs/getting-started/installation-%26-config

当我使用`$_GET`变量时,我期望得到以下结果:

Array(
    [query] => docs/getting-started/installation-&-config
)

然而,我得到的结果是:

Array(
    [query] => docs/getting-started/installation-
    [-config] => 
)

是否可以在.htaccess中对查询进行编码?如果不能,我该如何解决这个问题?
英文:

I have this final rule in .htaccess

RewriteRule ^(.*)$ index.php?query=$1 [NC,L,QSA]

Then I enter the url:

domain.com/docs/getting-started/installation-%26-config

When I use $_GET variable, this is the result I expect:

Array(
    [query] => docs/getting-started/installation-&-config
)

However, this is the result I get

Array(
    [query] => docs/getting-started/installation-
    [-config] => 
)

Is it possible to encode the query from .htaccess? If not, how can I resolve this?

答案1

得分: 2

> RewriteRule ^(.*)$ index.php?query=$1 [NC,L,QSA]

需要包括B标志以重新编码捕获的反向引用,因为RewriteRule的模式匹配的URL路径已进行URL解码。例如:

RewriteRule (.*) index.php?query=$1 [B,L,QSA]

(这里不需要NC标志。RewriteRule模式上也不需要起始和结束锚点。)

参考链接:

英文:

> RewriteRule ^(.*)$ index.php?query=$1 [NC,L,QSA]

You need to include the B flag to re-encode the captured backreference, since the URL-path that the RewriteRule pattern matches against is URL-decoded. For example:

RewriteRule (.*) index.php?query=$1 [B,L,QSA]

(The NC flag is not required here. Neither are the start and end-of-string anchors on the RewriteRule pattern.)

Reference:

huangapple
  • 本文由 发表于 2023年5月25日 16:52:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定