将 Codeigniter 4 安装在子目录中并保留域通用 URL 结构

Ken*_*ara 2 .htaccess codeigniter

现在的情况:

\n\n

我已经在 上安装了 WordPress,http://example.com并且该站点是多语言的,因此您可以在 访问英文网页http://example.com/en/something-here/,也可以在 访问该网页的西班牙语版本http://example.com/es/algo-aqui/

\n\n

我正在尝试做的事情:

\n\n

我想安装 Codeigniter 4,http://example.com/software/但我想保留一般网站 URL 结构,因此我需要能够通过 URLhttp://example.com/en/software/http://example.com/es/software/. 始终阅读本段开头提到的相同 CI4 安装。

\n\n

我已经尝试过的:

\n\n

我部署了 CI4 文件http://example.com/software/并修改了.htaccessWordpress 站点和 Code Igniter 的文件。

\n\n

这是我的.htaccess根目录下的 WP 文件:

\n\n
# Disable directory browsing\nOptions All -Indexes\n\n<IfModule mod_rewrite.c>\nRewriteEngine On\n    RewriteBase /\n    RewriteCond %{REQUEST_FILENAME} !-f\n    RewriteCond %{REQUEST_FILENAME} !-d\n    RewriteRule ^[a-z]{2}/software(.*)$ /software/index.php$2 [L]\n</IfModule>\n\n# BEGIN WordPress\n# Las directivas (l\xc3\xadneas) entre `BEGIN WordPress` y `END WordPress` se generan din\xc3\xa1micamente\n# , y solo se deber\xc3\xadan modificar mediante filtros de WordPress.\n# Cualquier cambio en las directivas que hay entre esos marcadores se sobreescribir\xc3\xa1n.\n<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteBase /\nRewriteRule ^index\\.php$ - [L]\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule . /index.php [L]\n</IfModule>\n\n# END WordPress\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我.htaccess在“软件”目录中的文件:

\n\n
# Disable directory browsing\nOptions All -Indexes\n\n# ----------------------------------------------------------------------\n# Rewrite engine\n# ----------------------------------------------------------------------\n\n# Turning on the rewrite engine is necessary for the following rules and features.\n# FollowSymLinks must be enabled for this to work.\n<IfModule mod_rewrite.c>\n    Options +FollowSymlinks\n    RewriteEngine On\n\n    # If you installed CodeIgniter in a subfolder, you will need to\n    # change the following line to match the subfolder you need.\n    # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase\n    RewriteBase /\n\n    # Redirect Trailing Slashes...\n    RewriteCond %{REQUEST_FILENAME} !-d\n        RewriteRule ^(.*)/$ /$1 [L,R=301]\n\n    # Rewrite "www.example.com -> example.com"\n    RewriteCond %{HTTPS} !=on\n    RewriteCond %{HTTP_HOST} ^www\\.(.+)$ [NC]\n    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]\n\n    # Checks to see if the user is attempting to access a valid file,\n    # such as an image or css document, if this isn\'t true it sends the\n    # request to the front controller, index.php\n    RewriteCond %{REQUEST_FILENAME} !-f\n    RewriteCond %{REQUEST_FILENAME} !-d\n    RewriteRule (.*) /software/index.php/$1\n\n    # Ensure Authorization header is passed along\n    RewriteCond %{HTTP:Authorization} .\n    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]\n</IfModule>\n\n<IfModule !mod_rewrite.c>\n    # If we don\'t have mod_rewrite installed, all 404\'s\n    # can be sent to index.php, and everything works as normal.\n    ErrorDocument 404 index.php\n</IfModule>\n\n# Disable server signature start\n    ServerSignature Off\n# Disable server signature end\n
Run Code Online (Sandbox Code Playgroud)\n\n

我收到的错误:

\n\n

我可以访问网站和软件。该网站运行良好,但是当我尝试访问 Codeigniterhttp://example.com/en/software/时,我收到 404 错误。错误消息显示:“找不到控制器或其方法:App\\Controllers\\En::software”。

\n\n

这就好像 CI 试图找到控制器En(不存在)和方法software

\n\n

任何帮助将不胜感激。

\n

Zah*_*mon 6

经过2天的不懈努力,我已经解决了这个问题。这是我实际遵循的步骤-

首先以 root 身份输入以下命令来备份 Codeigniter4 应用程序:

tar --exclude=".git" -czvf zipped_file_name.tar codeigniter4_project_folder/
Run Code Online (Sandbox Code Playgroud)

将 codeigniter4_project_folder 上传到 Shared Hosing 的 public_html 目录中,并更改以下内容:

  1. 更改 app/Config/App.php 中的基本 url 或 codeigniter 应用程序根文件夹中的 .env 文件(此处 ci4 是稍后将在 cpanel 中配置的子域)

    app.baseURL = 'http://ci4.yourdomain.com/'

  2. 更改根文件夹中 app/Config/Database.php 或 .env 文件中的数据库配置
    database.default.hostname = localhost
    database.default.database = your_db_name
    database.default.username = your_db_user
    database.default.password = your_db_password
    database.default.DBDriver = MySQLi

  3. 更改公共文件夹中的 .htaccess 文件
    RewriteBase /codeigniter4_project_folder/

  4. 在 Cpanel ci4.yourdomain.com 中添加子域名,路径如下: public_html/codeigniter4_project_folder/public

这里公用文件夹将是路径中的最后一个。这是非常重要的。希望这可以帮助其他人解决这个问题。