【问题标题】:SEO url gives 404-error in CodeIgniterSEO url 在 CodeIgniter 中给出 404 错误
【发布时间】:2011-06-23 08:17:47
【问题描述】:

我对 codeigniter 还是很陌生。我知道php。

如何才能加载正确的视图?

我的网址:/blog/this-is-my-title

我已经告诉控制器类似

如果 end($this->uri->segment_array()) 确实存在于 DB 中,则将此数据加载到某个视图中。

每次访问 /blog/whatever 时都会收到 404 错误

我看错了什么?

【问题讨论】:

  • 如果您需要特定帮助,请提供您当前使用的代码

标签: php codeigniter codeigniter-url


【解决方案1】:

除非您使用路由,否则 url /blog/this-is-my-title 将始终为 404,因为 CI 正在寻找一种名为 this-is-my-title 的方法,而该方法当然不存在。

一个快速的解决方法是将您的帖子显示代码放入另一个函数并编辑 URL 以访问帖子,例如:/blog/view/the-post-title

这样的路线:

$route['blog/(:any)'] = "blog/view/$1";

如果您希望 URI 保持为 `/blog/this-is-my-title',也可以实现您想要的效果

【讨论】:

    【解决方案2】:

    可能还有更多的可能性:

    1. 最常见的 - mod_rewrite 未激活
    2. .htaccess 配置不正确(如果你没有编辑它试试 /blog/index.php/whatever)
    3. 控制器不存在或放错文件夹

      建议:如果您只需要更改数据,请使用同一控制器中的另一个视图

      如果(某事) {

      $this->load->view('whatever');

      }

      其他

      { $this->load->view('somethingelse'); }

      如果这些作品都没有发布 .htaccess 的代码和配置示例,我会看看。

    【讨论】:

      【解决方案3】:

      解决此问题的最佳方法是重新映射控制器。这样,您仍然可以使用同一个控制器来做其他事情。

      无需路由!

      enter code here
      <?php
      class Blog extends Controller {
      function __construct()
      {
          parent::__construct();
      }
      public function _remap($method, $params = array())
      {
          if (method_exists($this, $method)) 
          {
              $this->$method();
          }
          else 
          {
              $this->show_post();
          }
      }
      function index()
      {
          // show blog front page
          echo 'blog';
      }
      function edit()
      {
          // edit blog entry
      }
      function category()
      {
          // list entries for this category
      }
      function show_post()
      {
          $url_title = $this->uri->segment(2);
          // get the post by the url_title
          if(NO RESULTS)
          {
              show_404();
          }
          else
          {
              // show post
          }
      }
        }
       ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-23
        • 2014-05-19
        • 1970-01-01
        • 2020-09-01
        • 2013-12-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多