I'm working in a CodeIgnitier project. We have many data with a wrong configuration so many URL contains unicode chars and others undesirables chars. Clean all data will take several months and I want to avoid 400 errors due to permitted_uri_chars. The ressources are accessibles if I remove undesirables chars. I would like automatically remove these chars. I try to put the following code in application/config/routes.php :
if ($this->uri->total_segments()) {
foreach (range(1, $this->uri->total_segments()) as $i) {
if ($this->uri->segment($i)) {
$this->uri->segments[$i] = preg_replace("#[^".$this->config->item('permitted_uri_chars')."]#i", "", $this->uri->segments[$i]);
}
}
}
But I get the 400 error. CodeIgniter controls URLs before passing in routes.php. Where can I put my code to avoid 400 error ? Can I do that in pre_system hook ?
Please understand this code and put on top into your header file. You can manage your url as you want.
$id = $this->uri->segment(3);
if($id)
{
$url = str_replace('/'.$id,"",$_SERVER['REQUEST_URI']);
} else {
$url = $_SERVER['REQUEST_URI'];
}
$pattern = '/([A-Z]+)/';
if(preg_match($pattern, $url))
{
$new_url = strtolower($url);
Header( 'HTTP/1.1 301 Moved Permanently' );
if($id){
Header( 'Location: ' . $new_url.'/'.$id );
} else {
Header( 'Location: ' . $new_url );
}
exit;
}
I use a solution to avoid 301 redirections. I don't want Google hate me because I send sitemaps with several 301 redirections. I allow certains chars but remove these in routes file.
In my config.php file :
$config['permitted_removed_uri_chars'] = '\',ß';
$config['permitted_uri_chars'] = 'a-z 0-9%.:_\-+&'.$config['permitted_removed_uri_chars'];
In my routes.php files :
if ($this->uri->total_segments()) {
foreach (range(1, $this->uri->total_segments()) as $i) {
if ($this->uri->segment($i)) {
$this->uri->segments[$i] = preg_replace("#[".$this->config->item('permitted_removed_uri_chars')."]#i", "", $this->uri->segments[$i]);
}
}
}