【问题标题】:CodeIgniter routing always through index.php?CodeIgniter 路由总是通过 index.php?
【发布时间】:2012-04-15 12:26:28
【问题描述】:

CodeIgniter 初学者在这里。我网站的基本 URL 是“http://localhost/routing/”。

// config.php
$config['base_url'] = 'http://localhost/routing/';

我只是尝试使用以下规则将 url 'http://localhost/routing/admin' 路由到管理控制器,但它不起作用。相反,我必须使用'http://localhost/routing/index.php/admin'。

$route['default_controller'] = 'seasons';
$route['admin'] = 'admin';
$route['404_override'] = '';

问题:有没有办法从网址中删除“index.php”?

【问题讨论】:

    标签: codeigniter routing


    【解决方案1】:

    在 .htaccess 文件和 config.php 中进行更改

    application/config.php

    $config['index_page'] = '';
    

    .htaccess

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php/$0 [PT,L]
    

    【讨论】:

    • 这个答案很好,但完成这项工作的最后一个技巧是将 .htaccess 从:“yourprojectfolder/application/.htaccess”移动到“yourprojectfolder/.htaccess”
    【解决方案2】:

    有没有办法从 url 中删除 'index.php'?

    是的,除了是very popular question on SO,它还被in the CodeIgniter documentation 覆盖(非常好,我强烈推荐阅读)。

    【讨论】:

    • 我不得不使用以下规则,尽管 'RewriteRule (.*)$ index.php' 而不是 'RewriteRule ^(.*)$ /index.php/$1 [L]'。跨度>
    • 很高兴你在这里提供了链接,尽管我认为 CodeIgniter 团队应该删除这个奇怪的要求,或者至少在他们的入门教程中解释一下,而不是像谁想要那样假装它看起来很正常在他们的网址中间有“index.php”开始......:S
    • @XunYang 我也在想同样的事情。但是,它让我对路由进行了更多研究。
    【解决方案3】:

    在您的.htaccess 中写下这个。还可以查找更多信息:Codeigniter Guide

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|robots\.txt) # exceptions
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 2013-02-16
      • 2012-03-30
      • 2016-10-10
      相关资源
      最近更新 更多