.htaccess中的Codeigniter URL路由问题

时间:2015-06-15 01:26:14

标签: php apache .htaccess codeigniter-2

在您将其标记为重复FIY之前,我已经尝试了所有可以在SO上找到的解决方案。

网址为www.deltadigital.ca

配置文件(如果我使用$ 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>

它给了我webhost的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,主机是1和1,我的视图文件在views / tlc文件夹中

更新:删除了前导斜杠: $route['([a-z]+)'] = "tlc/view/$1";

1 个答案:

答案 0 :(得分:2)

好的,如前所述我不是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>

(在写完答案后,我发现您目前没有最后一条规则,如图所示。)