每个网址都有MVC index.php

时间:2014-06-22 15:21:17

标签: php codeigniter codeigniter-routing

我今天第一次开始使用codeigniter,虽然我之前使用过asp.net mvc。我想知道为什么我的URL一直需要index.php?

示例:

http://localhost/index.php/mozilla-developing-chromecast-competitor-that-runs-firefox-os-report

我的控制器中没有名为index.php的函数。我很确定这是一个愚蠢的问题,但我一直试图解决它超过一个小时,我仍然无能为力。

路线:

$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/home';

控制器

class Pages extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('pages_model');
}

public function home()
{
    $data['blog'] = $this->pages_model->get_blog();
    $data['title'] = 'Blog archive';

    $this->load->view('templates/header', $data);
    $this->load->view('pages/home', $data);
    $this->load->view('templates/footer');
}

public function about()
{
    $data['title'] = 'About us';

    $this->load->view('templates/header', $data);
    $this->load->view('pages/about', $data);
    $this->load->view('templates/footer');
}

public function view($slug)
{
    $data['blog_item'] = $this->pages_model->get_blog($slug);

    if (empty($data['blog_item']))
    {
        show_404();
    }

    $data['title'] = $data['blog_item']['title'];

    $this->load->view('templates/header', $data);
    $this->load->view('pages/view', $data);
    $this->load->view('templates/footer');
}
}

HTAaccess

Deny from all

1 个答案:

答案 0 :(得分:2)

在codeigniter中查看URL-Routing:http://www.technicalkeeda.com/details/how-to-create-seo-friendly-url-using-php-codeigniter也请检查一下,确保WebRoot中的.htaccess正常。 http://kevinthompson.info/blog/2011/03/04/completely-remove-index-php-from-expressionengine-urls.html。这应该可以帮到你。

或基本设置:

Options +FollowSymlinks
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

<IfModule mod_rewrite.c>

# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /

# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]

# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

</IfModule>