.htaccess URL路由在CodeIgniter 4中抛出400 Bad Request错误

pnwntuvh  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(149)

在无数次被其他用户的问题帮助之后,我终于要问我的第一个问题了。
"我想做的是“
在实时服务器上运行CodeIgniter项目(版本4.2.8)。

问题:

URL路由不起作用(400错误请求错误)。这是一个全新的安装,因此除了默认的CodeIgniter主页(显示“欢迎使用CodeIgniter”的主页)之外没有其他页面。
当我按如下方式访问欢迎页面时,它会正确显示:
www.example.com
www.example.com/index.php
尝试以下操作时出现错误请求:
www.example.com/home
"这是我所做的“
服务器上的文件夹结构:

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...

我已经调整了以下文件:

  • 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 . '../example/app/Config/Paths.php';
// ^^^ Change this line if you move your application folder

一开始我总是收到一个500内部服务器错误。经过几个小时的搜索解决方案,我遇到了这个职位:
CodeIgniter4 htaccess won't work with Options All -Indexes
这确实解决了500内部服务器错误,欢迎页面显示。我也确认了我的主机提供商,结果他们不允许选项全部和跟随符号链接。
我很确定htaccess文件中有一些东西需要调整,但我不知道是什么。
下面是完整的htaccess文件:

  • 公众_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

如果有人能告诉我需要改变什么才能让它工作,那就太棒了。

ldfqzlk8

ldfqzlk81#

您可以在配置文件中尝试此设置吗?设置基本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;

相关问题