I’m using CodeIgniter 3.1.13 and configured my htaccess file to remove “index.php” but when I try to go to a webpage it doesn’t work unless I put the default controller in the URL. Any advice?
Going to the following URLs work fine…
- https://www.example.com
- https://www.example.com/index.php
- https://www.example.com/welcome
- https://www.example.com/welcome/page/test
But the following URL does not work…
How do I get this URL to work?
Here’s my htaccess file…
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] #force https RewriteCond %{HTTP_HOST} example.com [NC] RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://example.com/$1 [R,L] </IfModule> <IfModule !mod_rewrite.c> # Without mod_rewrite, route 404's to the front controller ErrorDocument 404 /index.php </IfModule>
I changed my config.php to this…
$config['base_url'] = 'https://www.example.com/'; $config['index_page'] = '';
Here’s my Welcome.php controller…
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Welcome extends CI_Controller { function __construct() { parent::__construct(); } function index() { $this->load->view('homepage', $data); } function page($pageName) { $data['content'] = $this->load->view($pageName, '', TRUE); $this->load->view('template_page', $data); } }
Advertisement
Answer
$route['default_controller']
only specifies which controller gets executed when the root directory of the website is requested. That is: when no controller/method
subdirectory/string is given. In your example that would be when www.example.com
or www.example.com/index.php
are requested.
The default controller does not get executed when the requested url subdirectory/string starts with one of its methods.
For the www.example.com/page/test
url to work, you need to either have a Page
controller with a test
method in it:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Page extends CI_Controller { function __construct() { parent::__construct(); } function test() { $this->load->view('test'); } }
Or add the following to config/routes.php
to have the www.example.com/page/*
urls be handled by the Welcome
controller’s page
method:
$route['page/(:any)'] = 'welcome/page/$1';