【问题标题】:url_suffix in codeigniter not working properlycodeigniter 中的 url_suffix 无法正常工作
【发布时间】:2012-03-08 19:59:04
【问题描述】:

我是 Codeigniter 的新手,使用 Codeigniter 1.7,我在根目录中创建了一个带有代码的 .htaccess 文件:

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

从 url 中删除 index.php,并添加

$config['url_suffix'] = ".html";

到 config.php 文件为所有页面提供默认后缀。之后我用这段代码创建了一个控制器 test.php:

class Test extends Controller{

function test(){
    parent::Controller();
}

function index(){
    $this->load->view('test');
}

}

还有一个视图:

<?php
    echo anchor(current_url(), 'This Page');
?>

当我导航到 http://localhost/ci/test.html 时,它工作正常,但是当我点击视图中由 anchor() 函数自动生成的链接时,它转到 http://localhost/ci/index.php/test.html

如何从 anchor() 函数生成的 url 中删除 /index.php/?

当我指向主页时

localhost/ci/index.html

它显示了一个 404 Page not found 错误,但是当我指向时

localhost/ci/index.php

它工作正常。为什么主页没有被转换为 index.html 而不是 index.php?

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    你必须用 htaccess 创建一个重写条件。 要删除 index.php,请将其添加到根文件夹 .htaccess 文件中

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

    如果您将加载测试作为您的默认控制器,那么这将是您的基本网址

    本地主机/ci/ 要么 本地主机/ci/test/ 要么 本地主机/ci/test/index.html

    确保将您的 config.php 设置为此

    $config['index_page'] = ''
    

    您还应该使用 Codeigniter 2.1 以获得适当的 php 5 支持。

    如果使用 php 5,您的构造函数也应该是这样的。否则,请坚持使用 php 4 所拥有的功能。

    public function __construct()
    {
        parent::__construct();
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-22
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 2013-01-15
      相关资源
      最近更新 更多