0

Followed the steps of another SO question on how to remove index.php from CI3.0 urls. Everything works fine except other pages now throw 404.

URL Before: domain.com/index.php/about.php After: domain.com/about.php <- Throw 404

The default page however still works but as soon as i try to navigate to another page, 404...

Controller:

<?php
class Pages extends CI_Controller
{

    public function view($page = 'home')
    {

        if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php')) {

            show_404();

        }

        $data['title'] = ucfirst($page);

        $this->load->helper('url');
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);

    }

}

Router:

$route['default_controller'] = 'pages/view';
$route['about'] = 'pages/view/about';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Config:

$config['index_page'] = '';

.htaccess:

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
B. Desai
  • 16,414
  • 5
  • 26
  • 47
Sqveeze
  • 73
  • 6
  • And what if you try - domain.com/about ( No .php on the end ) – TimBrownlaw Nov 19 '17 at 10:10
  • same result. 404 – Sqveeze Nov 19 '17 at 13:37
  • You should have had this worked out by now... If you put echo APPPATH.'views/pages/'.$page.'.php'; as your first line in your view method and maybe comment out your show_error(); what do you get? What is the actual path and filename its looking for? – TimBrownlaw Nov 19 '17 at 13:49
  • followed your steps but the echo only displays the path on the main page, when i go to any other page then its throw 404 without the path on the top. – Sqveeze Nov 19 '17 at 18:32
  • What is the path to your .htaccess file you've been modifying. It looks like an existing CI .htaccess, or did you just copy it when you created it? It has to be in the same folder as your index.php file. – TimBrownlaw Nov 19 '17 at 22:06
  • If u use linux or ubuntu , Serevr apache2 . See Link [Set AllowOverride all](https://stackoverflow.com/questions/18740419/how-to-set-allowoverride-all) – Jignesh Nov 20 '17 at 05:57
  • TimBrownlaw, thanks the error was because i copied the rules into the .htaccess located in the application folder. Now i added the rules to an .htaccess in the folder where index.php located and everything works fine. Anyway to mark your comment as answer? – Sqveeze Nov 20 '17 at 21:31

2 Answers2

0

Right from the 1st day of using CodeIgniter 3, I've been using the following .htaccess file to remove index.php. I never faced issued with URLs. I feel this can help you.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule>

I've many projects online which are working without any problem, though I don't have complete knowledge on mod_rewrite.

BEingprabhU
  • 1,618
  • 2
  • 21
  • 28
  • I believe this code is good, but somehow after adding this, urls with index.php still works, and if i try urls without index.php codeigniter not even throw its own 404 page, just the default browser one. – Sqveeze Nov 19 '17 at 18:30
  • If it works with `index.php` without any issue, then it is absolutely fine. The main moto is to remove `index.php` from the URLs. You should avoid using it when you are creating a URL. (http://iamgoodatcoding.com/controller/function) – BEingprabhU Nov 19 '17 at 18:43
  • If you are getting browser default 404 page, then you are missing something. Check with your controller and function names. – BEingprabhU Nov 19 '17 at 18:48
0

In case you are on Ubuntu, edit the file /etc/apache2/apache2.conf (here we have an example of /var/www):

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

and change it to;

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Then ,

sudo a2enmod rewrite

Restart your apache servser

sudo service apache2 restart

I think it's Done.

Jignesh
  • 11
  • 1
  • 2