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

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

Preserve ampersand sign in URL from .htaccess

问题

  1. 我在.htaccess中有这个最终规则:
  2. RewriteRule ^(.*)$ index.php?query=$1 [NC,L,QSA]
  3. 然后我输入URL
  4. domain.com/docs/getting-started/installation-%26-config
  5. 当我使用`$_GET`变量时,我期望得到以下结果:
  6. Array(
  7. [query] => docs/getting-started/installation-&-config
  8. )
  9. 然而,我得到的结果是:
  10. Array(
  11. [query] => docs/getting-started/installation-
  12. [-config] =>
  13. )
  14. 是否可以在.htaccess中对查询进行编码?如果不能,我该如何解决这个问题?
英文:

I have this final rule in .htaccess

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

Then I enter the url:

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

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

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

However, this is the result I get

  1. Array(
  2. [query] => docs/getting-started/installation-
  3. [-config] =>
  4. )

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解码。例如:

  1. 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:

  1. 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:

确定