Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Codeigniter Codeigntier:config/routes.php文件中的重定向?_Codeigniter_Routes_Url Redirection - Fatal编程技术网

Codeigniter Codeigntier:config/routes.php文件中的重定向?

Codeigniter Codeigntier:config/routes.php文件中的重定向?,codeigniter,routes,url-redirection,Codeigniter,Routes,Url Redirection,我正在更新一个网站,有一堆旧的URL需要重新路由到它们的新等价物。我想在服务器端执行此操作(而不是使用.htaccess) 如果我能在我的config/routes.php中声明如下内容,那就太好了: $route['old_url_1'] = redirect('/new/url/1', 'location', 301); $route['old_url_2'] = redirect('/new/url/2', 'location', 301); $route['old_url_3'] = r

我正在更新一个网站,有一堆旧的URL需要重新路由到它们的新等价物。我想在服务器端执行此操作(而不是使用.htaccess)

如果我能在我的config/routes.php中声明如下内容,那就太好了:

$route['old_url_1'] = redirect('/new/url/1', 'location', 301);
$route['old_url_2'] = redirect('/new/url/2', 'location', 301);
$route['old_url_3'] = redirect('/new/url/3', 'location', 301);
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Redirectme extends MY_Controller {

    public function index($url=false) {
     if($url===false)
         redirect('/');
     redirect('/new/url/'.$url,'location',301);
    }

}

?>
很明显,它不起作用,但是像这样的事情是可能的,例如,将这些代码保存在我的路由文件中(更符合逻辑),还是我需要在一些控制器中设置函数


谢谢。

是的,您需要创建一些东西(控制器上的方法),用301代码重定向浏览器。大概是这样的:

$route['old_url_1'] = redirect('/new/url/1', 'location', 301);
$route['old_url_2'] = redirect('/new/url/2', 'location', 301);
$route['old_url_3'] = redirect('/new/url/3', 'location', 301);
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Redirectme extends MY_Controller {

    public function index($url=false) {
     if($url===false)
         redirect('/');
     redirect('/new/url/'.$url,'location',301);
    }

}

?>

无论如何,最好通过.htaccess重定向(因为它甚至不会从Web服务器调用php模块)。

好的,谢谢。但无法保存路由文件?@Inigo否,如果您想使用默认CI路由系统,则只能将VAR发送到某些控制器的方法。您可以自己定义要发送的变量
$route['my/old/url.html']=“mystore/item/apple”例如好的,谢谢。现在我还有一个该死的问题。一些旧的URL中有空格,而且(当然)codeigniter的路由不能处理空格。是的,但是谢谢你的帮助。。。找到了一个解决方法。@Inigo尝试使用%20作为空格,或使用
(:any)
通配符将所有URL段发送到方法(例如
$route[“旧URL/(:any)/(:any)”]=“mystore/redirect/$1/$2”