【问题标题】:Codeigniter: Remove controller from URL and disable access through the original pathCodeigniter:从 URL 中删除控制器并禁用通过原始路径的访问
【发布时间】:2016-04-22 06:48:53
【问题描述】:

原始网址

mydomain.com/main/portfolio

现在我需要使用以下路由从 URL 中删除控制器名称,我已成功完成:

//Controller Name: main
$route['(:any)'] = "main/$1";

我的新网址很好:

mydomain.com/portfolio

但我不希望出于 SEO 目的访问原始 URL:

mydomain.com/main/portfolio

如何让我的好网址正常运行并阻止旧网址。

谢谢。

【问题讨论】:

  • 你可以使用 .htaccess 来做这个

标签: codeigniter


【解决方案1】:

您可以创建一个未找到的页面并将不需要的路由重定向到该页面。 例如,您可以创建错误控制器:

class Error extends CI_Controller {     
    public function error_404() {
        // send 404 header
        $this->output->set_status_header('404');
        // load a custom not found page view template
        $this->load->view('error_404');
    }        
}

然后在 routes.php 中,您可以像这样捕获那些包含“main/”的 URL:

// this should be placed above the (:any) route
$route['main/(:any)'] = 'error/error_404';
$route['(:any)'] = "main/$1";

【讨论】:

  • SEO 可以吗?
【解决方案2】:
RedirectMatch "^/main/(.*)" "http://www.example.com/$1"

更多示例可用here。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多