英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论