0
votes

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 i.e. 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['([a-z]+)'] = "tlc/view/$1";

1
Surely the base URL should be blank? CI obtains that for you... - Mike Rockétt
Thanks, but still doesn't work. I tried other rewrite rules after setting to blank. So is this a problem with the server settings? - Vegan Sv
Hmmm, perhaps remove the leading slash from your second route? Documentation says it is important to leave those out. Also, your base URL should be defined with the trailing slash: $config['base_url'] = "http://www.deltadigital.ca/". - Mike Rockétt
Yes, I added the trailing slash ...www.deltadigital.ca/ Ive tried different rules I found online and the one Im using now is RewriteRule ^(.*)$ index.php?/$1 [L,QSA] - so it has no leading slash anyway, still doesn't work. - Vegan Sv
If I remove the leading slash its giving me 500 server error which indicates there is an error in .htaccess, with the leading slash it's just giving me my webhosts 404. Thank you anyway - Vegan Sv

1 Answers

2
votes

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