简体   繁体   English

codeigniter不会重定向到utf-8 url

[英]codeigniter doesn't redirect to utf-8 url

I'm trying to implement search form to my website using Codeigniter 3.0 and I'm having some issues with redirect() method. 我正在尝试使用Codeigniter 3.0在我的网站上实现搜索表单,并且我在redirect()方法上遇到了一些问题。

What I want to do is when I type some string and press enter, it will do a post request to a controller then the controller will get the string and redirect current url to http://example.com/search/string 我想做的是当我键入一些字符串并按Enter时,它将向控制器发出发布请求 ,然后控制器将获取字符串并将当前URL重定向到http://example.com/search/string

So, here are the codes that I have: 所以,这是我拥有的代码:

Search form: 搜索表格:

<form action="/search" method="post" accept-charset="UTF-8">

A controller which gets search string and do redirect: 一个获取搜索字符串并进行重定向的控制器:

public function do_search() {
    $search = $this->input->post('search');

    log_message('debug', 'search: ' . $search);

    if ($search) {
       redirect('search/' . $search, 'refresh');
    } else {
       redirect('/', 'refresh');
    }
}

config.php: config.php文件:

$config['permitted_uri_chars'] = 'üÜöÖğĞıİəƏçÇşŞ a-z 0-9~%.:_\-';

Also, I have following code on my base controller: MY_Controller 另外,我的基本控制器上有以下代码:MY_Controller

$this->output->set_header('Content-Type: text/html; charset=utf-8');

Now, when I type "həə" on my search form, it does redirect, but url becomes like "hÉÉ" . 现在,当我在搜索表单中键入“həə”时,它会重定向,但是url变得像“hÉÉ” As you see I'm logging search string on my controller, which prints correctly. 如您所见,我正在控制器上记录搜索字符串,该字符串可以正确打印。 (like "həə" ). (例如“həə” )。

Am I missing something? 我想念什么吗? Any ideas how to solve this? 任何想法如何解决这个问题?

Non ascii support is a general problem in software and it is a very special problem in browser related software. 不支持ascii是软件中的普遍问题,并且是与浏览器相关的软件中的一个非常特殊的问题。 What you should do is, fetch the incoming values and have a kind of logic and do a remap. 您应该做的是,获取传入的值,并具有某种逻辑并进行重新映射。 Codeigniter has a build in remap function. Codeigniter具有内置的重映射功能。

It is called by: 称为:

function _remap($method)
    {
      // here your logic

    }

You can have this function in every controller. 您可以在每个控制器中都具有此功能。

A small tipp: If you are supporting multi-languages, then you should transfer on every request the language in your GET or POST or Cookie and have a kind of logic. 一个小提示:如果您支持多语言,则应在每个请求上转移GET或POST或Cookie中的语言,并具有某种逻辑。 Otherwise you will have trouble. 否则您会遇到麻烦。

I wrote a small jQuery code to do redirection, and it did work in my case. 我写了一个小的jQuery代码进行重定向,在我的情况下它确实起作用。

$("form[name=search]").submit(function (e) {
  e.preventDefault();
  window.location = "/search/" + $("input[name=search]").val();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM