当前位置: 动力学知识库 > 问答 > 编程问答 >

url_suffix in codeigniter not working properly

问题描述:

I am new to Codeigniter and using Codeigniter 1.7, I created a .htaccess file in root directory with code:

RewriteEngine on

RewriteCond $1 !^(index\.php|images|robots\.txt)

RewriteRule ^(.*)$ /ci/index.php/$1 [L]

to remove index.php from urls, and added

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

to config.php file to provide default suffix for all pages. After that I created a controller test.php with this code:

class Test extends Controller{

function test(){

parent::Controller();

}

function index(){

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

}

}

and a view also:

<?php

echo anchor(current_url(), 'This Page');

?>

When I navigate to http://localhost/ci/test.html it works fine, but when I click the link auto generated by anchor() function in my view it goes to http://localhost/ci/index.php/test.html

How can I remove /index.php/ from urls generated by anchor() function?

Also when i point to home page

localhost/ci/index.html

it shows me a 404 Page not found error but when I point to

localhost/ci/index.php

it works fine. Why home page is not being converted to index.html instead of index.php?

网友答案:

You have to create a rewrite condition with htaccess. To remove index.php, add this to the root folder .htaccess file

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

If your loading Test as your default controller then this will be your base url

localhost/ci/ or localhost/ci/test/ or localhost/ci/test/index.html

Make sure to set your config.php to this

$config['index_page'] = ''

You should also be using Codeigniter 2.1 for proper php 5 support.

Also your constructor funciton should be like so if using php 5. Else stick with what you have for php 4.

public function __construct()
{
    parent::__construct();
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: