如何连接code使用mod_rewrite和放大器特殊字符; Apache的? [英] How to encode special characters using mod_rewrite & Apache?
问题描述
我想有pretty网址为我的标签系统与所有的特殊字符一起: +
,&安培;
,#
,%
和 =
。有没有办法用mod_rewrite的做到这一点,而无需增加一倍EN code中的链接?
我注意到,delicious.com和计算器似乎能够单独处理EN codeD特殊字符。什么是神奇公式?
下面是什么,我希望发生的一个例子:
http://www.foo.com/tag/c%2b%2b
会引发以下重写规则:
重写规则^标签/(.*)script.php的?标签= $ 1
和标签的价值将是C ++
阿帕奇/ mod_rewrite的正常运行不会像这样工作,因为它似乎把加号变成空格。如果我是双带code中的加号'%252B'然后我得到了想要的结果 - 但它使混乱的URL和似乎pretty哈克给我
。
阿帕奇/ mod_rewrite的正常运行不会像这样工作,因为它似乎把加号变成空格。
块引用>我不认为这是相当发生了什么。 Apache是在路径一部分%,以双品+ S解码,因为+是一个有效的字符那里。它在请求让mod_rewrite的外观之前做到这一点。
于是mod_rewrite的改变你的请求'/标签/ C ++到运行script.php?标签= C ++。但在应用程序/ x-WWW的形式连接codeD格式的查询字符串组成部分,排出的规则是那些在路部分应用很略有不同。特别是,'+'是空间的简写(这也可以同样是EN codeD为%20,但是这是一个古老的行为,我们将永远无法改变现在)。
所以PHP的形式读code接收C ++和转储它在你的_GET为C-空间的空间。
看起来解决这个方式是使用rewriteflagB。见<一href=\"http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteflags\">http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteflags - 好奇它使用或多或少相同的例子
重写规则^标签/(.*)$ /script.php?tag=$1 [B]
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?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.foo.com/tag/c%2b%2b
Would trigger the following RewriteRule:
RewriteRule ^tag/(.*) script.php?tag=$1
and the value of tag would be "c++"
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 hacky to me.
解决方案The normal operation of apache/mod_rewrite doesn't work like this, as it seems to turn the plus signs into spaces.
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.
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).
So PHP's form-reading code receives the 'c++' and dumps it in your _GET as C-space-space.
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]
这篇关于如何连接code使用mod_rewrite和放大器特殊字符; Apache的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!