在将index.php?
添加到 url 之前,我无法访问控制器中的其他函数。我可以直接访问任何控制器的index()
功能,但不能访问其他功能。我已经检查了其他相关问题,但问题仍然存在。我还尝试将所有控制器添加到routes.php
但仍然失败。
哈塔塞斯
RewriteEngine on
RewriteCond $1 !^(index.php|images|css|js|include|style.css|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
配置
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';
$config['base_path'] = $_SERVER['DOCUMENT_ROOT'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';
博客控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
}
public function index()
{
$data['page_name'] = 'blog';
$this->load->view('pages/index', $data);
}
public function news()
{
$data['page_name'] = 'news';
$this->load->view('pages/index', $data);
}
}
网址
http://localhost/tsb/blog // This works fine
http://localhost/tsb/blog/news // only works when i add index.php? to the url
您需要正确检查您的 css 和 js 包含。确保使用绝对网址。从您的问题来看,您似乎正在通过(pages)文件夹中的一个索引文件加载所有视图
您的资产或资源链接应如下所示
<link rel="stylesheet" type ="text/css" src="<?php echo base_url();?>css/style.css" />
对 JavaScript 或其他资产执行相同的操作
$config['base_url'] = 'http://localhost/tsb/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
HTACCESS
RewriteEngine on RewriteCond $1 !^(index.php|images|css|js|include|style.css|robots.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
这样,您的所有页面都将正确加载,包括控制器中的所有功能
打开配置.php并执行以下操作替换
$config['index_page'] = "index.php"
自
$config['index_page'] = ""
只需更换
$config['uri_protocol'] ="AUTO"
自
$config['uri_protocol'] = "REQUEST_URI"
更改自
$config['base_url'] = 'http://localhost/tsb/';
自
$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';
$config['base_path'] = $_SERVER['DOCUMENT_ROOT'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';
并在HTACCESS文件中放置以下代码
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
查找以下内容:
打开配置.php并执行以下操作替换
$config['index_page'] = "index.php"
自
$config['index_page'] = ""
在某些情况下,uri_protocol的默认设置无法正常工作。只需更换
$config['uri_protocol'] ="AUTO"
自
$config['uri_protocol'] = "REQUEST_URI"
这将从您的URL中删除索引.php,您可以在没有索引的情况下访问页面,方法等.php