[英]url routing throws 400 Bad Request error in CodeIgniter 4
在無數次其他網友的幫助下,我終於要問我的第一個問題了。
我想做什么:
在實時服務器上運行 CodeIgniter 項目(版本 4.2.8)。
問題:
URL 路由不工作(400 錯誤請求錯誤)。 這是全新安裝,因此除了默認的 CodeIgniter 主頁(上面寫着“歡迎使用 CodeIgniter”的那個)之外沒有其他頁面。
當我這樣訪問它時,歡迎頁面正確顯示:
www.example.com
www.example.com/index.php
我在嘗試時收到 Bad Request 錯誤:
這是我所做的:
服務器上的文件夾結構:
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...
我調整了以下文件:
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
起初我總是收到 500 Internal Server 錯誤。 經過數小時的尋找解決方案后,我發現了這篇文章:
CodeIgniter4.htaccess 不適用於 Options All -Indexes
這確實解決了 500 Internal Server 錯誤,歡迎頁面正在顯示。 我還與我的托管服務提供商確認過,事實證明他們不允許 Options All 和 FollowSymlinks。
我很確定 .htaccess 文件中有一些東西需要調整,但我不知道是什么。
這是完整的.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
如果有人能告訴我需要更改哪些內容才能使其正常工作,那就太棒了。
讓它運行。
原來我忘記在 spark 文件中進行必要的調整。
在這里更改了這部分:
/*
*---------------------------------------------------------------
* 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
你能在配置文件中試試這個嗎? 將基數 URL 設置為:
$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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.