.htaccess url编码的字符串未正确传递到页面 [英] .htaccess url encoded string not passing to page correctly

查看:79
本文介绍了.htaccess url编码的字符串未正确传递到页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.htaccess中有一个重写规则

I have a rewrite rule in .htaccess

RewriteRule ^page/(.*)$ my-php-page.php?cid=$1

我正在传递一个编码字符串,其中包含诸如=+之类的字符-这是编码字符串;

I am passing a encoded string which contains characters like = and + - this is the encoded string;

V1ihnKpip6WpnY7Wm5zn2+6YUtLf2KCh4eKY

在完整的URL中是

http://www.example.com/my-php-page.php?cid=V1ihnKpip6WpnY7Wm5zn2+6YUtLf2KCh4eKY

现在,由于+符号,此操作不起作用.所以我想发送cid urlencoded,然后变成

now this doesn't work because of the + sign. So I want to send the cid urlencoded which then becomes;

http://www.example.com/my-php-page.php?cid=V1ihnKpip6WpnY7Wm5zn2%2B6YUtLf2KCh4eKY

+符号变为%2B.这很好用,除非当我通过.htaccess中的RewriteRule尝试进行此操作时-它不起作用.因此,URL将是

The + sign becomes %2B. This works great, except when I try this through the RewriteRule in .htaccess - it doesn't work. So the URL would be

http://www.example.com/page/V1ihnKpip6WpnY7Wm5zn2%2B6YUtLf2KCh4eKY

由于某些原因,将%2B替换为空格,mypage.php文件实际上将cid接收为V1ihnKpip6WpnY7Wm5zn2 6YUtLf2KCh4eKY.

The mypage.php file actually receives the cid as V1ihnKpip6WpnY7Wm5zn2 6YUtLf2KCh4eKY for some reason replacing %2B with a blank space.

有什么想法可能会这样做吗?以及我该如何解决.非常感谢您的帮助.

Any ideas why it may be doing that? And how can I fix it. Many thanks for the help.

编辑

我刚刚找到了此解决方案- PHP $ _GET带有urlencode和"&"错误-但是我想知道.htaccess中是否有比不得不两次urlencode字符串更优雅的解决方案?

I just found this solution - PHP $_GET var with urlencode and "&" bug - but I was wondering if there was a more elegant solution in .htaccess than having to urlencode the string twice?

推荐答案

尝试添加B重写标志.此标志告诉mod_rewrite转义反向引用,文档对此表示:

Try adding the B rewrite flag. This flag tells mod_rewrite to escape backreferences, the documentation says this:

_rewrite在映射URL之前必须先对URL进行转义,因此反向引用将在应用时取消转义.使用B标志,反向引用中的非字母数字字符将被转义.

_rewrite has to unescape URLs before mapping them, so backreferences will be unescaped at the time they are applied. Using the B flag, non-alphanumeric characters in backreferences will be escaped.

由于%字符保留用于对RewriteRule之前的RewriteCond语句中匹配的分组进行反向引用,因此mod_rewrite将它们区别对待,并可能最终尝试用空白的反向引用替换%2.

Since the % character is reserved for backreferences to groupings matched in RewriteCond statements preceeding the RewriteRule, mod_rewrite treats them differently and could end up attempting to replace the %2 with a blank backreference.

所以您的规则应如下所示:

So your rule should look like this:

RewriteRule ^page/(.*)$ my-php-page.php?cid=$1 [B]

这篇关于.htaccess url编码的字符串未正确传递到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆