.htaccess 中的 Codeigniter URL 路由问题
Codeigniter URL routing issues in .htaccess
在你将它标记为重复的 FIY 之前,我已经尝试了我能在 SO 上找到的所有解决方案。
配置文件(如果我使用 $config['base_url'] = 'http://www.deltadigital.ca' - 它根本不起作用)
//$config['base_url'] = 'http://www.deltadigital.ca';
$root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
.htaccess 文件
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond !^(index\.php|assets|woff|eot|img|css|js|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/index/?$ [L,R=301]
</IfModule>
它给我的虚拟主机的 404 错误。我在 SO 上尝试了其他解决方案,它要么给我 500 服务器错误,要么给我 codeidniter 的 404 错误
routes.php
$route['default_controller'] = "tlc/view";
$route['/([a-z]+)'] = "tlc/view/";
$route['404_override'] = '';
这是我的控制器
class Tlc extends CI_Controller
public function view($page='index')
{
if ( ! file_exists(APPPATH.'/views/tlc/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$this->load->view('tlc/templates/header.php');
$this->load->view('tlc/'.$page);
$this->load->view('tlc/templates/footer.php');
}
所以基本上我正在尝试使菜单链接起作用。它们只适用于完整的 url 即 deltadigital.ca/index.php/tlc/view/about-us
它是CI 2.2.2,主机是1and1,我的视图文件在views/tlc文件夹
更新:删除了前导斜杠:
$route['([a-z]+)'] = "tlc/view/";
好的,如前所述,我不是 CodeIgniter 大师。我所知道的是以下对我有用:
配置:
$config['base_url'] = "http://www.deltadigital.ca/";
# or use $config['base_url'] = "";
$config['uri_protocol'] = "REQUEST_URI";
$config['index_page'] = '';
路线:
$route['default_controller'] = "tlc/view";
$route['(:any)'] = "tlc/view/";
$route['404_override'] = "";
控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Tlc extends CI_Controller {
public function view($page='index')
{
if ( ! file_exists(APPPATH.'/views/tlc/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$this->load->view('tlc/templates/header.php');
$this->load->view('tlc/'.$page);
$this->load->view('tlc/templates/footer.php');
}
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/tlc.php */
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# Remove /index/
RewriteRule ^(.*)/index/?$ [L,R=301]
# Remove trailing slashes (prevents duplicate SEO issues)
RewriteRule ^(.+)/$ [L,R=301]
# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/ [L]
# If not a file or directory, route everything to CI
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
# RewriteRule ^(.*)$ index.php/ [L] # This is an alternative
</IfModule>
(写下我的回答后,我发现您目前没有最后一条规则,如图所示。)
在你将它标记为重复的 FIY 之前,我已经尝试了我能在 SO 上找到的所有解决方案。
配置文件(如果我使用 $config['base_url'] = 'http://www.deltadigital.ca' - 它根本不起作用)
//$config['base_url'] = 'http://www.deltadigital.ca';
$root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
.htaccess 文件
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond !^(index\.php|assets|woff|eot|img|css|js|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/index/?$ [L,R=301]
</IfModule>
它给我的虚拟主机的 404 错误。我在 SO 上尝试了其他解决方案,它要么给我 500 服务器错误,要么给我 codeidniter 的 404 错误
routes.php
$route['default_controller'] = "tlc/view";
$route['/([a-z]+)'] = "tlc/view/";
$route['404_override'] = '';
这是我的控制器
class Tlc extends CI_Controller
public function view($page='index')
{
if ( ! file_exists(APPPATH.'/views/tlc/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$this->load->view('tlc/templates/header.php');
$this->load->view('tlc/'.$page);
$this->load->view('tlc/templates/footer.php');
}
所以基本上我正在尝试使菜单链接起作用。它们只适用于完整的 url 即 deltadigital.ca/index.php/tlc/view/about-us
它是CI 2.2.2,主机是1and1,我的视图文件在views/tlc文件夹
更新:删除了前导斜杠:
$route['([a-z]+)'] = "tlc/view/";
好的,如前所述,我不是 CodeIgniter 大师。我所知道的是以下对我有用:
配置:
$config['base_url'] = "http://www.deltadigital.ca/";
# or use $config['base_url'] = "";
$config['uri_protocol'] = "REQUEST_URI";
$config['index_page'] = '';
路线:
$route['default_controller'] = "tlc/view";
$route['(:any)'] = "tlc/view/";
$route['404_override'] = "";
控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Tlc extends CI_Controller {
public function view($page='index')
{
if ( ! file_exists(APPPATH.'/views/tlc/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$this->load->view('tlc/templates/header.php');
$this->load->view('tlc/'.$page);
$this->load->view('tlc/templates/footer.php');
}
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/tlc.php */
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# Remove /index/
RewriteRule ^(.*)/index/?$ [L,R=301]
# Remove trailing slashes (prevents duplicate SEO issues)
RewriteRule ^(.+)/$ [L,R=301]
# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/ [L]
# If not a file or directory, route everything to CI
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
# RewriteRule ^(.*)$ index.php/ [L] # This is an alternative
</IfModule>
(写下我的回答后,我发现您目前没有最后一条规则,如图所示。)