【问题标题】:Codeigniter page routing not workingCodeigniter 页面路由不起作用
【发布时间】:2015-03-29 09:40:31
【问题描述】:

我正在使用 CodeIgniter 3,但在让路由器在我的开发环境中工作时遇到问题。文件的相关部分:

站点/.htaccess 文件:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L] 

site/application/config/routes.php 文件:

$route['default_controller'] = 'auth/login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['page/(:any)'] = 'page/view/$1';

站点/应用程序/config/config.php:

$config['base_url'] = 'http://localhost/~me/site/';
$config['index_page'] = '';

站点/应用程序/控制器/Page.php

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends CI_Controller {
public function view($page = 'about')
{
    if (!$this->ion_auth->logged_in()) {
        redirect('auth/login');
    }
    if ( ! file_exists(APPPATH.'/views/page/'.$page.'.php')) {
        show_404();
    }
    $this->load->view('templates/header');
    $this->load->view('page/'.$page);
    $this->load->view('templates/footer');
}

结果:

localhost/~me/site takes me to login as expected (routes.php is being called)
localhost/~me/site/index.php/page/view/about works (basic logic works)
localhost/~me/site/index.php/page/about works ($route['page/(:any)'] works) 

但是,我想使用的是:localhost/~me/site/page/about 给出:

The requested URL /index.php/page/about was not found on this server

我在 localhost/~me/site/page/view/about 中遇到同样的错误

所以似乎只有在以下情况下才能到达 routes.php: a) 显式调用 index.php,或 b) 在没有任何扩展的情况下调用路由

任何帮助将不胜感激!

【问题讨论】:

    标签: php .htaccess codeigniter


    【解决方案1】:

    在你的 .htaccess 文件中试试这个

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]
    

    【讨论】:

    • 我之前尝试过这个文件配置,但是当我的服务器备份时,我会用我当前的配置再试一次。我昨天所做的一些编辑显然在关机/重启后弄乱了我的访问权限 (stackoverflow.com/questions/28239817/…)
    • 我已经完全重新安装了操作系统(我也遇到了其他异常情况)。服务器似乎再次运行良好,但我仍然遇到相同的结果。上面推荐的 .htaccess 文件仅导致将错误消息中的引用更改为 /index.php 而不是 /index.php/page/about。在这一点上,我想知道这是否可能是 OS X Yosemite 和/或 Apache 2.4 的问题。
    【解决方案2】:

    您是否已将 AllowOverride All 指令添加到您的 Apache 配置文件或虚拟主机配置文件中?

    【讨论】:

      猜你喜欢
      • 2011-11-07
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多