Welcome Guest, Not a member yet? Register   Sign In
Trying to remove index.php from url in latest codeigniter 4.5.1
#1

Hi
I've started a new codeigniter 4.5.1 appstarter project at my PC and am having difficult time to remove the index.php from uri

1) This is the virtualhost set in my Apache. Points at public as suggested

Code:
<VirtualHost *:80>
    ServerAdmin admin@localhost
    ServerName sw
    ServerAlias sw
    DocumentRoot /media/data/services/public
    <Directory /media/data/services/public>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error_sw.log
    CustomLog ${APACHE_LOG_DIR}/access_sw.log combined
</VirtualHost>

2) I've enabled the mod_rewrite, restarted apache, edited /etc/hosts file. Then I browse to http://sw , which shows the codeigniter welcome, great!

3) I rename the root/env file to .env and leave it with

Code:
#--------------------------------------------------------------------
# APP
#--------------------------------------------------------------------
CI_ENVIRONMENT = development
app.baseURL = 'http://sw'
app.indexPage = ''

4) I define a new route in app/config/Routes.php

Code:
$routes->get('/services', 'Services::index');

Then, my controller app/Controllers/Services.php looks like

PHP Code:
<?php

namespace App\Controllers;

class 
Services extends BaseController
{
    public function index(): string
    
{
        return view('view_services');
    }


Then, when I try this url in browser

http://sw/services

I'm getting 404 ...

5) Tried also replacing the public/.htaccess with the suggested in
https://codeigniter.com/user_guide/gener...x-php-file
No luck

6) I've also tried setting the baseURL, indexPage, etc. options in app/Config/App.php without luck

I've got no ideas left to solve this
Thanks for any suggestion
Reply
#2

Does the file 'view_services' exist in the app/Views folder?
Reply
#3

Try go in App/Config/app then

PHP Code:
public string $indexPage 'index.php';  /*===*/  /*remove index.php*/ public string $indexPage ''
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#4

(05-02-2024, 08:57 AM)kabeza Wrote: Then, when I try this url in browser

http://sw/services

I'm getting 404 ...

The exact error message matters.
If Apache returns the error before running CI4, CI4 cannot do anything.
Reply
#5

(05-02-2024, 09:44 AM)Bosborne Wrote: Does the file 'view_services' exist in the app/Views folder?

Yes. When using index.php in the url, I've checked the controller/view worked fine



(05-02-2024, 03:51 PM)luckmoshy Wrote: Try go in App/Config/app then
PHP Code:
public string $indexPage 'index.php';  /*===*/  /*remove index.php*/ public string $indexPage ''

Yes, as stated in original post, I've tried that in .env file and app/Config/App.php also, without luck



(05-02-2024, 05:18 PM)kenjis Wrote:
(05-02-2024, 08:57 AM)kabeza Wrote: Then, when I try this url in browser

http://sw/services

I'm getting 404 ...

The exact error message matters.
If Apache returns the error before running CI4, CI4 cannot do anything.

Yes, the url works great with index.php but when I remove it Apache throws 404. Gonna check if I can do anything related to Apache and will update in case I succeed. Thanks
Reply
#6

Another way : Go to Public folder, move (or copy) htaccess and index.php to the main app folder. Then in your main app/index.php find the line 34 and change the path like

PHP Code:
require FCPATH 'app/Config/Paths.php'
Reply
#7

If you want to open a page like http://sw/services
you have to remove the "/" in the Config/Routes.php

like this:
Code:
$routes->get('services', 'Services::index');
Reply
#8

(05-03-2024, 05:31 AM)demyr Wrote: Another way : Go to Public folder, move (or copy) htaccess and index.php to the main app folder. Then in your main app/index.php find the line 34 and change the path like

PHP Code:
require FCPATH 'app/Config/Paths.php'

The virtualhost is pointing (as recommended in user_guide) to the public folder, so moving/copying the index.php and .htaccess from public to app/ didn't work


(05-08-2024, 02:56 AM)elbambolo Wrote: If you want to open a page like http://sw/services
you have to remove the "/" in the Config/Routes.php

like this:
Code:
$routes->get('services', 'Services::index');

With or without "/" didn't work

I've finally solved it by getting the appStarter again working from scratch in another folder/virtualhost
Then moved to the non-working application all the files, mainly the ones from app/Config and again overwriting everything from /vendor/codeigniter4/framework/app/Config

Don't know which file from app/Config was making conflict, but maybe I've touched or edited something that didn't make it work

Thanks 2 everyone
Reply




Theme © iAndrew 2016 - Forum software by © MyBB