简体   繁体   中英

Codeigniter URL routing issues in .htaccess

Before you mark it as duplicate FIY I have tried all solutions I could find on SO.

The url is www.deltadigital.ca

config file (if I use $config['base_url'] = ' http://www.deltadigital.ca ' - it doesnt work at all)

//$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 file

<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>

It's giving me my webhost's 404 error. I tried other solutions on SO and it's either giving me 500 server error or codeidniter's 404 error

routes.php

$route['default_controller'] = "tlc/view";
$route['/([a-z]+)'] = "tlc/view/$1";
$route['404_override'] = '';

And this is my controller

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');   
        }

So basically Im trying to make menu links work. They only work with full url ie deltadigital.ca/index.php/tlc/view/about-us

It's CI 2.2.2, host is 1and1, my view files are in views/tlc folder

update: removed the leading slash: $route['([az]+)'] = "tlc/view/$1";

Okay, as stated before I am not a CodeIgniter guru. What I do know is that the following works for me:

Config:

$config['base_url'] = "http://www.deltadigital.ca/";
# or use $config['base_url'] = "";
$config['uri_protocol'] = "REQUEST_URI";
$config['index_page'] = '';

Routes:

$route['default_controller'] = "tlc/view";
$route['(:any)'] = "tlc/view/$1";
$route['404_override'] = "";

Controller:

<?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>

(Upon writing my answer, I see that you do not currently have the last rule, as shown.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM