Welcome Guest, Not a member yet? Register   Sign In
Codeigniter4 404 Not found on server
#1

Hey,

I just finished my codeigniter project today. Everything worked fine on my local computer, but when I uploaded it to a hosting server I always get a 404 not found error.
My flder structure is the following:
-app
-public
-system
-tests
-writable
-.env
-.htaccess
-index.php
-spark


My htaccess contains the following code:
# Disable directory browsing
Options All -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On

# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod...ewritebase
# RewriteBase /

# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 index.php
</IfModule>

# Disable server signature start
ServerSignature Off
# Disable server signature end



Would be nice if someone could help me out Smile
Reply
#2

so little information, would better if show your router and controller.
Reply
#3

(This post was last modified: 11-12-2022, 02:00 AM by risk.)

Herein is my routes code:

<?php

namespace Config;

// Create a new instance of our RouteCollection class.
$routes = Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (is_file(SYSTEMPATH . 'Config/Routes.php')) {
require SYSTEMPATH . 'Config/Routes.php';
}

/*
* --------------------------------------------------------------------
* Router Setup
* --------------------------------------------------------------------
*/
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
// The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps
// where controller filters or CSRF protection are bypassed.
// If you don't want to define all routes, please use the Auto Routing (Improved).
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
// $routes->setAutoRoute(false);

/*
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');

//routes

$routes->get('(?i)login', 'Auth::login');
$routes->match(['get', 'post'], '(?i)loginUser', 'Auth::loginUser');
$routes->get('(?i)logout', 'Auth::logout');

$routes->match(['get', 'post'], '(?i)upload-image', 'Upload::uploadImage');

$routes->match(['get', 'post'], '(?i)Image/albums/(:any)', 'Image::albums/$1');
$routes->get('(?i)image/addAlbum/(:num)/(:any)', 'Image::addAlbum/$1/$2');

$routes->get('(?i)all-images', 'Image::allImages');
$routes->get('(?i)all-albums', 'Albums::allAlbums');
$routes->get('(?i)load-image/(:num)/(:any)', 'Image::loadImage/$1/$2');
$routes->get('(?i)album/(:any)', 'Albums::loadAlbum/$1');


/*
* --------------------------------------------------------------------
* Additional Routing
* --------------------------------------------------------------------
*
* There will often be times that you need additional routing and you
* need it to be able to override any defaults in this file. Environment
* based routes is one such time. require() additional route files here
* to make that happen.
*
* You will have access to the $routes object within that file without
* needing to reload it.
*/
if (is_file(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}




And which controller do you mean?

(11-12-2022, 01:59 AM)risk Wrote: Here is my routes code:

<?php

namespace Config;

// Create a new instance of our RouteCollection class.
$routes = Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (is_file(SYSTEMPATH . 'Config/Routes.php')) {
    require SYSTEMPATH . 'Config/Routes.php';
}

/*
* --------------------------------------------------------------------
* Router Setup
* --------------------------------------------------------------------
*/
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
// The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps
// where controller filters or CSRF protection are bypassed.
// If you don't want to define all routes, please use the Auto Routing (Improved).
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
// $routes->setAutoRoute(false);

/*
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');

//routes

$routes->get('(?i)login', 'Auth::login');
$routes->match(['get', 'post'], '(?i)loginUser', 'Auth::loginUser');
$routes->get('(?i)logout', 'Auth::logout');

$routes->match(['get', 'post'], '(?i)upload-image', 'Upload::uploadImage');

$routes->match(['get', 'post'], '(?i)Image/albums/(:any)', 'Image::albums/$1');
$routes->get('(?i)image/addAlbum/(:num)/(:any)', 'Image::addAlbum/$1/$2');

$routes->get('(?i)all-images', 'Image::allImages');
$routes->get('(?i)all-albums', 'Albums::allAlbums');
$routes->get('(?i)load-image/(:num)/(:any)', 'Image::loadImage/$1/$2');
$routes->get('(?i)album/(:any)', 'Albums::loadAlbum/$1');


/*
* --------------------------------------------------------------------
* Additional Routing
* --------------------------------------------------------------------
*
* There will often be times that you need additional routing and you
* need it to be able to override any defaults in this file. Environment
* based routes is one such time. require() additional route files here
* to make that happen.
*
* You will have access to the $routes object within that file without
* needing to reload it.
*/
if (is_file(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
    require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}




And which controller do you mean?
Reply
#4

The only thing i would say is how and in which way did you get it working on localhost - what was your approach? If you were using say sqlite for db and had it on your Desktop, and firing up using spark I can see your structure working . If you had your Ci4 as a sub-directory inside a web server then you would have needed to do something like :

Code:
<VirtualHost 127.0.0.7:80>
    ServerAdmin [email protected]
    DocumentRoot "/srv/http/kursaal2.org/public"
    ServerName kursaal2.org
    ServerAlias kursaal2.org
    ErrorLog "/var/log/httpd/kursaal2.org-error_log"
    CustomLog "/var/log/httpd/kursaal2.org-access_log" common

    <Directory "/srv/http/kursaal2.org/public">
       Order allow,deny
    Allow from All
    AllowOverride All
    Require all granted
    </Directory>
</VirtualHost>

kursaal2.org is the name of my CI4 web app inside main web server root. I reach the landing page by going to

Code:
127.0.0.7:80

On live same directory structure(sub domains inside my hosting space) as local except i use cPanel so that everything is served from public of each web app.

I never have had to touch htaccess on either local or live. If your CI4 web app is going to be the only one on your hosting , then the approach used to be have everything at the web server web root that is currently inside your public folder. PAck everything else into one directory and have that at one level about your web root on live. Then edit index.php inside public so things point to your directory containing everything else
CMS CI4     I use Arch Linux by the way 

Reply
#5

(This post was last modified: 11-12-2022, 03:41 AM by risk.)

I had it in the htdocs folder of XAMPP and used a maria DB database. Like I said: everything worked fine on localhost but on the server nothing execpt the start site works.
To open the site on localhost i needes the url: localhost/my-site
On the server its: ip/mysite
Reply
#6

(11-12-2022, 02:50 AM)risk Wrote: I had it in the htdocs folder of XAMPP and used a maria DB database. Like I said: everything worked fine on localhost but on the server nothing execpt the start site works.
To open the site on localhost i needes the url: localhost/my-site
On the server its: ip/mysite

Might be off base on this, but check your folders in the controller folder tree.  On a linux box you have to use the first letter capitalized like the controller file.  I had an api folder that worked on the local host (Windows) and I had to change it to Api to make the ajax calls work.

Ken
Reply




Theme © iAndrew 2016 - Forum software by © MyBB