通过文本输入编码URL的特殊字符有问题

时间:2022-09-04 12:29:08

I'm building a PHP application using CodeIgniter. It is similar to Let Me Google That For You where you write a sentence into a text input box, click submit, and you are taken to a URL that displays the result. I wanted the URL to be human-editable, and relatively simple. I've gotten around the CodeIgniter URL routing, so right now my URLs can look something like this:

我正在使用CodeIgniter构建一个PHP应用程序。它类似于Let Me Google That For You,您将一个句子写入文本输入框,单击提交,然后您将转到显示结果的URL。我希望URL是人类可编辑的,而且相对简单。我已经了解了CodeIgniter URL路由,所以现在我的URL看起来像这样:

http://website.com/?q=this+is+a+normal+url

The problem right now is when the sentence contains a special character like a question mark, or a backslash. Both of these mess with my current .htaccess rewrite rules, and it happens even when the character is encoded.

现在的问题是当句子包含一个特殊字符,如问号或反斜杠时。这两个都与我当前的.htaccess重写规则相混淆,甚至在字符编码时也会发生。

http://website.com/?q=this+is+a+normal+url? OR
http://website.com/?q=this+is+a+normal+url%3F

What does work is double-encoding. For example, if I take the question mark, and encode it to %253F (where the ? is encoded to %3F and the % sign is encoded to %25). This url works properly.

什么工作是双重编码。例如,如果我采用问号,并将其编码为%253F(其中?编码为%3F,%符号编码为%25)。此网址正常。

http://website.com/?q=this+is+a+normal+url%253F

Does anyone have an idea of what I can do here? Is there a clever way I could double encode the input? Can I write a .htaccess rewrite rule to get around this? I'm at a loss here. Here are the rewrite rules I'm currently using for everyone.

有谁知道我能在这做什么?有没有一种聪明的方法可以对输入进行双重编码?我可以写一个.htaccess重写规则来解决这个问题吗?我在这里不知所措。以下是我目前正在为大家使用的重写规则。

RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^(.*)$ /index.php/app/create/%{QUERY_STRING}? [L]

Note: The way CodeIgniter works is they have a index/application/function/parameter URL setup. I'm feeding the function the full query string right now.

注意:CodeIgniter的工作方式是它们具有索引/应用程序/功能/参数URL设置。我现在正在为函数提供完整的查询字符串。

2 个解决方案

#1


If your’re using Apache 2.2 and later, you can use the B flag to force the backreference to be escaped:

如果您使用的是Apache 2.2及更高版本,则可以使用B标志强制对反向引用进行转义:

RewriteCond %{QUERY_STRING} ^q=.*
RewriteRule ^ /index.php/app/create/%0? [L,B]

#2


I usually do human readable urls like this

我通常做这样的人类可读网址

$humanReadableUrl= implode("_",preg_split('/\W+/', trim($input), -1, PREG_SPLIT_NO_EMPTY)); 

It will remove any non-word characters and will add underscores beetween words

它将删除任何非单词字符,并将在单词之间添加下划线

#1


If your’re using Apache 2.2 and later, you can use the B flag to force the backreference to be escaped:

如果您使用的是Apache 2.2及更高版本,则可以使用B标志强制对反向引用进行转义:

RewriteCond %{QUERY_STRING} ^q=.*
RewriteRule ^ /index.php/app/create/%0? [L,B]

#2


I usually do human readable urls like this

我通常做这样的人类可读网址

$humanReadableUrl= implode("_",preg_split('/\W+/', trim($input), -1, PREG_SPLIT_NO_EMPTY)); 

It will remove any non-word characters and will add underscores beetween words

它将删除任何非单词字符,并将在单词之间添加下划线