如何使用 mod_rewrite & 对特殊字符进行编码阿帕奇? [英] How to encode special characters using mod_rewrite & Apache?

查看:29
本文介绍了如何使用 mod_rewrite & 对特殊字符进行编码阿帕奇?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的标记系统提供漂亮的 URL 以及所有特殊字符:+&#%=.有没有办法用 mod_rewrite 做到这一点,而不必对链接进行双重编码?

I would like to have pretty URLs for my tagging system along with all the special characters: +, &, #, %, and =. Is there a way to do this with mod_rewrite without having to double encode the links?

我注意到delicious.com 和stackoverflow 似乎能够处理单独编码的特殊字符.神奇的公式是什么?

I notice that delicious.com and stackoverflow seem to be able to handle singly encoded special characters. What's the magic formula?

这是我想要发生的事情的一个例子:

Here's an example of what I want to happen:

http://www.example.com/tag/c%2b%2b

将触发以下 RewriteRule:

Would trigger the following RewriteRule:

RewriteRule ^tag/(.*)   script.php?tag=$1

并且标签的值将是c++"

and the value of tag would be "c++"

apache/mod_rewrite 的正常操作不是这样的,好像是把加号变成了空格.如果我将加号双重编码为​​%252B",那么我会得到想要的结果 - 但是它会使 URL 变得凌乱,而且对我来说似乎很糟糕.

The normal operation of apache/mod_rewrite doesn't work like this, as it seems to turn the plus signs into spaces. If I double encode the plus sign to '%252B' then I get the desired result - however it makes for messy URLS and seems pretty hack to me.

推荐答案

apache/mod_rewrite 的正常操作不是这样的,好像是把加号变成了空格.

The normal operation of apache/mod_rewrite doesn't work like this, as it seems to turn the plus signs into spaces.

我认为这不是正在发生的事情.Apache 正在将路径部分中的 %2Bs 解码为 +s,因为 + 是那里的有效字符.它在让 mod_rewrite 查看请求之前执行此操作.

I don't think that's quite what's happening. Apache is decoding the %2Bs to +s in the path part since + is a valid character there. It does this before letting mod_rewrite look at the request.

然后 mod_rewrite 将您的请求 '/tag/c++' 更改为 'script.php?tag=c++'.但是在 application/x-www-form-encoded 格式的查询字符串组件中,转义规则与应用于路径部分的规则略有不同.特别是,+"是空格的简写(它也可以编码为%20",但这是一种我们现在永远无法改变的旧行为).

So then mod_rewrite changes your request '/tag/c++' to 'script.php?tag=c++'. But in a query string component in the application/x-www-form-encoded format, the escaping rules are very slightly different to those that apply in path parts. In particular, '+' is a shorthand for space (which could just as well be encoded as '%20', but this is an old behaviour we'll never be able to change now).

因此 PHP 的表单读取代码接收c++"并将其作为 C 空间空间转储到您的 _GET 中.

So PHP's form-reading code receives the 'c++' and dumps it in your _GET as C-space-space.

看起来解决这个问题的方法是使用重写标志'B'.参见 http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteflags - 奇怪的是它使用了或多或少相同的例子!

Looks like the way around this is to use the rewriteflag 'B'. See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteflags - curiously it uses more or less the same example!

RewriteRule ^tag/(.*)$ /script.php?tag=$1 [B]

这篇关于如何使用 mod_rewrite & 对特殊字符进行编码阿帕奇?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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