After being helped by other users’ questions uncountable times, I finally have to ask my first question.
What I’m trying to do:
Running a CodeIgniter project (ver. 4.2.8) on a live server.
Problem:
The URL routing is not working (400 Bad Request error).
It’s a fresh installation, so there are no pages other than the default CodeIgniter homepage (the one that says “Welcome to CodeIgniter”).
The welcome page displays correctly when I access it like this:
www.example.com
www.example.com/index.php
I get the Bad Request error when I try:
Here's what I've done:
Folder structure on server:
root
|
| dir public_html
| |
| dir www.example.com //web root, this is where I put the files that were in the CI public folder
| dir exampleCI4 //here I put all the other CI files and folders app, vendor, writable...
I have adjusted following files:
- exampleCI4/app/Config/App.php
public $baseURL = 'http://www.example.com'
// Load our paths config file
// This is the line that might need to be changed, depending on your folder structure.
require FCPATH . '../exampleCI4/app/Config/Paths.php';
// ^^^ Change this line if you move your application folder
At first I always got a 500 Internal Server error. After hours of searching for solutions I came across this post:
CodeIgniter4 htaccess won't work with Options All -Indexes
That indeed did solve the 500 Internal Server error, and the welcome page is displaying. I also confirmed with my hosting provider and it turns out that they don't allow Options All and FollowSymlinks.
I'm pretty sure there is something in the htaccess file that needs to be adjusted but I don't know what.
Here's the complete htaccess file:
- public_html/www.example.com/.htaccess
#Disable directory browsing
Options -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
Options SymLinksIfOwnerMatch
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/mod_rewrite.html#rewritebase
# 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
If somebody could tell me what needs to be changed in order for it to work, that would be awesome.
CodePudding user response:
Can you try this in the config file? Set the base URL like:
$base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$base_url .= "://". @$_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $base_url;
CodePudding user response:
Got it running.
Turned out I had forgotten to make the necessary adjustments in the spark file.
Changed this part here:
/*
*---------------------------------------------------------------
* BOOTSTRAP THE APPLICATION
*---------------------------------------------------------------
* This process sets up the path constants, loads and registers
* our autoloader, along with Composer's, loads our constants
* and fires up an environment-specific bootstrapping.
*/
// Load our paths config file
// This is the line that might need to be changed, depending on your folder structure.
require FCPATH . 'app/Config/Paths.php';
// ^^^ Change this line if you move your application folder