[英]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 $1 !^(index\.php|assets|woff|eot|img|css|js|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
</IfModule>
这给了我我的虚拟主机的404错误。 我在SO上尝试了其他解决方案,它要么给我500个服务器错误,要么给我Codeidniter 404错误
routes.php
$route['default_controller'] = "tlc/view";
$route['/([a-z]+)'] = "tlc/view/$1";
$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');
}
所以基本上我试图使菜单链接起作用。 它们仅适用于完整网址,即deltadigital.ca/index.php/tlc/view/about-us
它是CI 2.2.2,主机是1and1,我的视图文件在views / tlc文件夹中
更新:删除了斜杠: $route['([az]+)'] = "tlc/view/$1";
好的,正如我之前所说,我不是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/$1";
$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/?$ $1 [L,R=301]
# Remove trailing slashes (prevents duplicate SEO issues)
RewriteRule ^(.+)/$ $1 [L,R=301]
# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [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/$1 [L] # This is an alternative
</IfModule>
(在写下答案时,我看到您当前没有最后一条规则,如图所示。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.