【问题标题】:CodeIgniter: Rewrite URL with method parameter as subdomain?CodeIgniter:使用方法参数重写 URL 作为子域?
【发布时间】:2016-06-03 20:58:23
【问题描述】:

我有一个 CI 项目,我想在其中更改 URL,如下所示:

www.example.com/index.php/Controller/method/param1param1.example.com/index.php/Controller/method

这可能只使用 CI 吗?喜欢路线。或者我们需要.htaccess? 如果可能,是否存在任何风险或者这是一种不好的做法? 对SEO也有影响吗?

编辑:这种重写应该只发生在 Controller 控制器上。

编辑:我认为意思是错误的。当用户输入一个 URL 时 param1.example.com/Controller/method 它必须触发Controller 中的method 并将param1 作为参数传递。

【问题讨论】:

    标签: php .htaccess codeigniter url-rewriting


    【解决方案1】:
    1. 您需要创建重定向控制器 (controllers/Redirect.php),它将重定向浏览器 像这样:
    <?php
      defined('BASEPATH') OR exit('No direct script access allowed');
    
      class Redirect extends CI_Controller {
    
          public function index($method,$domain) {
              $redirect_url =  'http://www.' . $domain . '.example.com/index.php/Controller/' . $method;
              redirect($redirect_url,'refresh');
          }
    
      }
    ?>
    
    1. 在application/config/route.php中加入这一行

    $route['Controller/(:any)/(:any)'] = 'redirect/index/$1/$2';

    你已经完成了。享受:)

    【讨论】:

    • 这是一个重定向。我没有子域。我需要动态修改 URL 使其看起来像它在子域中。请检查问题的第二次编辑。谢谢
    • 那你应该使用.htaccess
    猜你喜欢
    • 2019-12-05
    • 2019-02-07
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多