问题
I am a complete beginner in CodeIgniter and have been following some videos and CI docs to get started. I did as these resources said, I added the .htaccess
file as the videos said. This is the file.
RewriteEngine on
RewriteBase /ciBlog/
RewriteCond $1 !^(index\.php|asstes|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
I set the base_url
to localhost/ciBlog
, tried the localhost/ciBlog/
as well. index_page
is set to ''
.
Neither can I get rid of the index.php
in the url, nor does anything load on the page I created following the correct procedure as described by docs and tutorials.
header.php
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ciBlog</title>
</head>
<body>
footer.php
</body>
</html>
home.php
<p>This is home.</p>
I'm trying to access localhost/ciblog/index.php/pages/view/home
(shouldn't have index.php
ideally). All I get is a blank page, nothing in page source either.
Pages.php
controller
<?php
class Pages extends CI_Controller {
public function view ( $page='home' ) {
if (!file_exists(APPPATH.'/views/pages/'.$page.'.php')) {
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('templates/header');
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer');
}
}
What am I doing wrong? I tried this in Ubuntu, thought I'm not being able to configure it with Ubuntu. Installed Windows just for this. All that time wasted to no avail. Any help appreciated. Tried with WAMP, XAMPP, and LAMP.
Update
I could remove index.php
. It just worked actually, I didn't do anything.
Update 2
Seems like I was making a silly mistake. I was running XAMPP server while updating WAMP files. Sorry for wasting your time.
回答1:
Put this code in your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?/index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 ?/index.php
</IfModule>
Correct the baseurl in your config file as below:
http://localhost/ciblog
header.php
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ciBlog</title>
</head>
<body>
footer.php
</body>
</html>
home.php
<?php
$this->load->view('templates/header');
echo "<p>This is home.</p>";
$this->load->view('templates/footer');
?>
Pages.php controller:
class Pages extends CI_Controller
{
public function view ( $page='home' )
{
$data['title'] = ucfirst($page);
$this->load->view('pages/'.$page, $data);
}
}
Now run with below url:
http://localhost/ciblog/pages/view/home
回答2:
use this code in your .htaccess file
and having without index.php in the url.
Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
回答3:
In config.php of config folder change the following
$config['base_url'] = 'http://localhost/project_name/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Change your htaccess file to the following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
In routes.php of config folder set the default controller as following
$route['default_controller'] = "your default controller";
and write error reporting in constructor for future debugging
function Controller() {
error_reporting(E_ALL);
parent::__construct();
}
来源:https://stackoverflow.com/questions/49731783/codeigniter-project-loads-nothing-on-the-localhost-browser