【问题标题】:Rewrite specific part of URL address重写 URL 地址的特定部分
【发布时间】:2018-12-11 15:43:36
【问题描述】:

我可以手动更改我的 Codeigniter 脚本中的每一段代码(这是巨大的),或者我可以使用 .htaccess 来完成。

有没有办法更改 URL 的一部分,将其屏蔽以使 www.page.com/athlete 变为 www.page.com/product?但是我不想进入代码,这意味着指向 www.page.com/athlete 的链接仍然可以工作,唯一的区别是在 URL 栏中它会显示 www.page.com/product

【问题讨论】:

    标签: php .htaccess codeigniter mod-rewrite


    【解决方案1】:

    您可以使用 Routes 来做到这一点。将此添加到 ./application/config/routes.php

    $route['product'] = 'athlete';
    

    现在,每当您访问 /products 时,它都会加载 /athlete 控制器。

    你也许可以尝试这样的事情。检查 URI 段是否为“athlete”,如果是,将它们重定向到您已设置为指向运动员控制器的“product”。像这样;

    public function __construct()
    {
        // Check if the URI is athlete, if it is, redirect them to product
        // But, product is this controller, which is defined in the routes
        if ($this->uri->segment(2) == 'athlete')
        {
            redirect('product');
        }
    }
    

    【讨论】:

    • 我明白了,非常优雅,但是我仍然需要更改网页上的链接,对吗?
    • 是的,您链接中的 URL 仍将链接到“运动员”控制器。
    • 您可以在类构造函数中检查 uri_segment 是什么,并将它们重定向到您喜欢的 uri。我会用一个例子来更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 2020-07-24
    • 2013-09-08
    • 2013-02-27
    • 2022-01-21
    • 2021-05-03
    • 2011-11-19
    • 2011-05-22
    • 2011-01-06
    相关资源
    最近更新 更多