如何使用mod_rewrite& Apache的? [英] How to encode special characters using mod_rewrite & Apache?

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

问题描述

我想为我的标签系统提供漂亮的网址以及所有特殊字符: + & = 。有没有办法这样做与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.foo.com/tag/c%2b%2b

将触发以下RewriteRule:

Would trigger the following RewriteRule:

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

标签的值为c ++

apache / mod_rewrite的正常操作不会像这样工作,因为它似乎把加号变成空格。如果我将加号加倍编码为'%252B',那么我会得到所需的结果 - 但是这样做会让我感到乱七八糟的URLS。

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.

推荐答案


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 ++'。但是在应用程序/ 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 ++',并将其转储到_GET中作为C空间。

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

看起来像这是使用rewriteflag'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& Apache的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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